• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Category and subcategory control which single post template should load?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Category and subcategory control which single post template should load?

This topic is: not resolved

Tagged: single post template

  • This topic has 2 replies, 2 voices, and was last updated 9 years, 3 months ago by kriskl.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • September 23, 2013 at 12:18 pm #63863
    kriskl
    Participant

    Hi,
    here is the scenario
    I have 2 (or more) categories and each has 5 (or more) subcategories

    cat1 - veg (subcat: 1) carrot 2) tomato etc
    cat2 - fruit (subcat: 1) apple 2) orange etc

    I have created for each category single templates:
    single-veg.php, single-fruit.php ..

    So, does anyone know what should be the correct function to load single-veg.php on all posts which belong to subcategories: 'veg', 'carrot' etc category??

    This is what I have adopted, but I think there must be a better way.. and of course, if you notice anything wrong with the code... I am newbie
    any help would be appreciated

    /** Get Post Category and sub category */
    
    function post_is_in_descendant_category( $cats, $_post = null )
    {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category');
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }
    
    /** Conditional Templates for Single posts */
    
    function template_change( $template ){
    
        if( is_single() && (post_is_in_descendant_category('12')) || in_category('12') ){
            $templates = array("single-veg.php");
    	}
    		
       elseif( is_single() && (post_is_in_descendant_category('17')) || in_category('17') ){
            $templates = array("single-fruit.php");
    		
        } elseif( is_single() && in_category('articles') ){
            $templates = array("single-articles.php");
       
        }
        $template = locate_template( $templates );
        return $template;
    }
    
    add_filter( 'single_template', 'template_change' ); //'template_include'/'single_template'
    
    
    November 20, 2013 at 1:03 pm #74467
    Susan
    Moderator

    Hi, as you posted this a while back, I hope you got your issue resolved. If not, please check back, and I will escalate for you. If it is resolved, please mark “resolved”, and I will close it. Thanks!

    December 4, 2013 at 11:20 am #76982
    kriskl
    Participant

    Thanks Susan

    Yes, it is resolved, I received help on another forum

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble