WordPress.org

Ready to get started?Download WordPress

Forums

If in subcategory of a category? (8 posts)

  1. hilj
    Member
    Posted 5 years ago #

    I would like to trigger stuff on my sidebar when the category 6 or it's subcategories are viewed.

    I could do it like this:

    <?php if (in_category('6') || in_category('7') in_category('8') in_category('9') in_category('10') in_category('11') in_category('12') { ?>
    do stuff
    <?php } ?>

    But it's lenghty, and in time I know I need to ad more subcategories and I would need to edit the template.

    in_category article touches the subject a bit. Saying that this should work:

    <?php in_category( '6' ) || post_is_in_descendant_category( 6 ) ?>

    But being such a neophyte I cant get it to work. All help is generously appreciated! :)

  2. MichaelH
    Member
    Posted 5 years ago #

    Try this:

    <?php
    // if a category view see if category is id=6 or 'direct child' of 6
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args = array(
        'include' => $cat,
    	  'hide_empty' => 0
    	  );
      $categories = get_categories($args);
      if ( ($cat == 6) || ($categories[0]->category_parent == 6) ) {
        echo 'you are viewing cat 6 or one of the DIRECT child';
      }
    }
    ?>
  3. hilj
    Member
    Posted 5 years ago #

    This almost solved my problem. When it's alone it works, but when I put two of them in a row the second one wont work. Like this:

    <?php // The first works works.
               if ( is_category() ) {
     			$cat = get_query_var('cat');
     			$args = array(
     			'include' => $cat,
     			'hide_empty' => 0
     			);
     			$categories = get_categories($args);
     			if ( ($cat == 1) || ($categories[0]->category_parent == 1) ) {
     			wp_list_categories('child_of=1&orderby=name&order=DESC&title_li=');
     			}
    
    			} elseif // The second wont work.
                 ( is_category() ) {
     			$cat = get_query_var('cat');
     			$args = array(
     			'include' => $cat,
     			'hide_empty' => 0
     			);
     			$categories = get_categories($args);
     			if ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
     	              wp_list_categories('child_of=18&orderby=name&order=DESC&title_li=');
     			} ?>

    Or in_category or is_category wont work after it.

    Thanks Michael! This was so far very helpful!

  4. MichaelH
    Member
    Posted 5 years ago #

    You wouldn't really need that 2nd set of code--

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args = array(
        'include' => $cat,
        'hide_empty' => 0
        );
      $categories = get_categories($args);
      if ( ($cat == 1) || ($categories[0]->category_parent == 1) ) {
        $child_of='child_of=1';
        } elseif ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
        $child_of='child_of=18';
        } else {
        $child_of='';
      }
      if ($child_of) {
        wp_list_categories($child_of .'&orderby=name&order=DESC&title_li=');
      }
    }
    ?>
  5. hilj
    Member
    Posted 5 years ago #

    Thanks again Michael! That solved it for me :)

  6. hilj
    Member
    Posted 5 years ago #

    One small issue arise while testing this.

    The following doesn't work in single.php file but works in archive and category templates.

    If this post is in subcategory x get the sidebar-x.php.

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args = array(
        'include' => $cat,
        'hide_empty' => 0
        );
      $categories = get_categories($args);
      if ( ($cat == 1) || ($categories[0]->category_parent == 1) ) {
        include(TEMPLATEPATH . '/sidebar.php');
        } elseif ( ($cat == 41) || ($categories[0]->category_parent == 41) ) {
        include(TEMPLATEPATH . '/sidebar02.php');
        } elseif ( ($cat == 18) || ($categories[0]->category_parent == 18) ) {
        include(TEMPLATEPATH . '/sidebar03.php');
        } else {
        get_sidebar();
      	}
    }
    ?>

    Is there a way around this?

    Thanks again! It's been so helpful so far!

  7. MichaelH
    Member
    Posted 5 years ago #

    You'll want to use the is_single() conditional. Also you need to get the categories (note plural) from the post itself...this example uses the FIRST category found on the post

    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $cat = $cats[0];
      }
      $args = array(
        'include' => $cat,
        'hide_empty' => 0
      );
    }
  8. Sam_a
    Member
    Posted 5 years ago #

    Just to clarify (there was some confusion in another thread about this) — you have to actually copy the definition of the post_is_in_descendant_category function mentioned in the Codex before you use it — it doesn't come with WordPress. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic