Forums

show sub categories whether on the category page or the sub category page (4 posts)

  1. deanes02
    Member
    Posted 2 years ago #

    The following code displays the sub pages of a main page whether you are on the main page or a sub page.

    I want to adapt it to show sub categories whether on the category page or the sub category page. The second set of code is where I am stuck.

    Thanks for any help!!!

    <?php
      //if you are on a page NOT a cat
      if ($post->ID) {
        //if you are on the sub page display the parents sub pages
        //if you are on a page display the sub pages
        if($post->post_parent)
          $children = wp_list_pages("title_li=&amp;child_of=".$post->post_parent."&amp;echo=0");
        else
          $children = wp_list_pages("title_li=&amp;child_of=".$post->ID."&amp;echo=0");
        if ($children) { ?>
          <ul>
          <?php echo $children; ?>
          </ul>
      <?php }} ?>
    <?php
      if ($_GET[cat]) {
        if($post->post_parent) //what is the category equivalent??
          $children = wp_list_categories("title_li=&amp;child_of=".$post->post_parent."&amp;echo=0");
        else
          $children = wp_list_categories("title_li=&amp;child_of=".$_GET[cat]."&amp;echo=0");
        if ($children) { ?>
          <ul>
          <?php echo $children; ?>
          </ul>
      <?php }} ?>
  2. deanes02
    Member
    Posted 2 years ago #

    For those who are interested ---

    <?php
    $cat = get_query_var('cat');
    $category = get_category ($cat);
    
    if ($category->cat_ID) {
    
    if($category->category_parent)
    $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->category_parent."&echo=0");
    else
    $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->cat_ID."&echo=0");
    if ($children) { ?>
    
          <?php echo $children; ?> 
    
    <?php }} ?>
  3. signyourbikeout
    Member
    Posted 2 years ago #

    Not sure if you ever got the recognition you deserve, but this worked great, out of the box.

    Can anyone help me modify it so that if there are no children, it echos nothing? I'm assume an elseif statement at the end, but I'm not yet strong enough in PHP to get the markup correct, though I am doing some trial and error.

  4. signyourbikeout
    Member
    Posted 2 years ago #

    For those interested:

    http://wordpress.org/support/topic/328775

    I found a solution to my above question...using str_replace to remove the "No categories" line that wp_list_pages returned on finding no children with the above code from deanes02.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.