Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'single_template', 'my_single_template' );
- function my_single_template($single_template)
- {
- if (in_category(1)) {
- $file = get_template_directory().'/single-cat-1.php';
- if ( file_exists($file) ) {
- return $file;
- }
- }
- return $single_template;
- }
- function wpse_category_single_template( $single_template ) {
- global $post;
- $all_cats = get_the_category();
- if ( $all_cats[0]->cat_ID == '1' ) {
- if ( file_exists(get_template_directory() . "/single-cat1.php") ) return get_template_directory() . "/single-cat1.php";
- } elseif ( $all_cats[0]->cat_ID == '2' ) {
- if ( file_exists(get_template_directory() . "/single-cat2.php") ) return get_template_directory() . "/single-cat2.php";
- }
- return $single_template;
- }
- add_filter( 'single_template', 'wpse_category_single_template' );
- function show_template() {
- global $template;
- print_r($template);
- }
- add_action( 'wp_head', 'show_template' );
- function wpse_category_single_template( $single_template ) {
- global $post;
- $all_cats = get_the_category();
- if ( in_category(6) ) {
- if ( file_exists(get_template_directory() . "/page-custom.php") ) {
- return get_template_directory() . "/page-custom.php";
- } else {
- return get_template_directory() . "/page.php";
- }
- }
- return $single_template;
- }
- add_filter( 'single_template', 'wpse_category_single_template' );
- add_filter( 'single_template', function ( $template )
- {
- // Get the current single post
- $post_id = $GLOBALS['wp_the_query']->get_queried_object_id();
- // Test to see if our post belongs to category 1
- if ( !in_category( 1, $post_id ) ) {
- return $template;
- }
- // Our post is attached to category 1, lets look for single-1.php
- $locate_template = locate_template( 'single-1.php' );
- // Test if our template exist, if so, include it, otherwise bail
- if ( !$locate_template ) {
- return $template;
- }
- return $locate_template;
- });
Add Comment
Please, Sign In to add comment