Created
January 31, 2018 00:29
-
-
Save anwas/5d034634b3b28cf4eed0882d121f9c10 to your computer and use it in GitHub Desktop.
[WordPress Include custom template] #wordpress #template #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Include Custom Templates | |
*************************************************************************** */ | |
function my_custom_template( $template ) { | |
// patikrinama ar rodomas puslapis yra pasirinktinio tipo įrašų archyvas | |
if ( ! is_admin() && is_post_type_archive( 'custom-post-type' ) ) { | |
// įtraukiami reikiami stiliai ir scenarijai | |
add_action( 'wp_enqueue_scripts', 'my_custom_styles' ); | |
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' ); | |
// nurodomas reikiamas failas priedo aplanke | |
$file_in_plugin = MY_PLUGIN_PATH . 'templates/archive-custom-post-type.php'; | |
// sudaromas failų masyvas, kurių bus ieškoma aktyvios temos aplanke | |
$theme_files = array( | |
'archive-custom-post-type.php', | |
'page-templates/archive-custom-post-type.php', | |
'templates/archive-custom-post-type.php' | |
); | |
// patikrinama, ar yra temos aplanke bent vienas failas | |
// grąžinamas pirmo pagal sąrašą surasto failo kelias arba tuščia eilutė | |
$exists_in_theme = locate_template( $theme_files, false ); | |
if ( '' !== $exists_in_theme ) { | |
// jei temos aplanke rastas failas, grąžinamas kelias iki jo | |
return $exists_in_theme; | |
} | |
else if ( file_exists( $file_in_plugin ) ) { | |
// jei temos aplanke nerastas failas, | |
// grąžinamas priedo aplanke esančio failo kelias, jei toks failas yra | |
return $file_in_plugin; | |
} | |
} | |
// jeigu nerastas nei vienas ieškomas failas temos ir priedo aplankuose, | |
// ar nebuvo atitikta nei viena tikrinimo sąlyga, | |
// grąžinamas standartinio šablono kelias | |
return $template; | |
} | |
if ( function_exists( 'add_filter' ) ) { | |
add_filter( 'template_include', 'my_custom_template', 99 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment