Forums

[resolved] Is subcategory of a given category (2 posts)

  1. nextlimits
    Member
    Posted 1 year ago #

    Hi guys,

    Here is the situation. I have a site composed of pages and posts and different categories.
    I want to show in the sidebar, a custom menu based on the new menu from WP 3.0.

    I am already showing it on all child pages of any given page (is_tree) but I would also want to be able to show it on categories that are added in the custom menu.

    <?php
    	// A second sidebar for widgets, just because.
    	if ( is_tree('8') ) : if ( is_active_sidebar( 'projects' ) ) : ?>

    So what I'm looking for is a function that will return true or false, if it is a subcategory of a defined category. Something that will end up like this:

    <?php
    	// A second sidebar for widgets, just because.
    	if ( is_tree('8') || is_subcategory('7') ) : if ( is_active_sidebar( 'projects' ) ) : ?>

    Thanks

  2. nextlimits
    Member
    Posted 1 year ago #

    Found the function: IS_SUBCATEGORY_OR_SUB

    // If is category or subcategory of $cat_id
    if (!function_exists('is_category_or_sub')) {
    	function is_category_or_sub($cat_id = 0) {
    	    foreach (get_the_category() as $cat) {
    	    	if ($cat_id == $cat->cat_ID || cat_is_ancestor_of($cat_id, $cat)) return true;
    	    }
    	    return false;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic