Support » Plugins and Hacks » Have wp_list_categories output the category description

  • carl-johan

    (@carl-johan)


    I’m using the following code to output a categories childs.
    Is there a way to have the code output the Category Description instead of the Category Name?

    <?php
    
    $current=get_query_var('cat');
    
    wp_list_categories ("show_count=0&hide_empty=0&title_li=&child_of=$current");
    
    ?>
Viewing 15 replies - 1 through 15 (of 20 total)
  • MichaelH

    (@michaelh)

    Should be able to convert this to your needs:

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
      foreach($categories as $term) {
        echo '<p>' . $title . '<a href="' . get_term_link( $term->term_id, $taxonomy ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
        echo 'Category description is:' . $term->description;
      }
    }
    ?>
    carl-johan

    (@carl-johan)

    Thanks a lot Michael, it worked almost all the way. I converted the code to this:

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
      if ($categories) {
        foreach($categories as $term) {
    echo '<a href="' . get_term_link( $term->term_id, $taxonomy ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
       }
      }
    ?>

    The output works fine, but it links to this:
    http://www.lula.se/?taxonomy=&term=
    which seem to just return my “home.php” template.

    The link should go to this page:
    http://www.lula.se/category/illustration/carl-johan-lindqvist-illustration

    which is:
    http://www.homesite.se/category-base/category-name/sub-category-name

    Any ideas?

    Thanks again for the help!

    MichaelH

    (@michaelh)

    Maybe change ‘get_term_link( $term->term_id, $taxonomy ) ‘ to `get_category_link( $term->term_id )

    carl-johan

    (@carl-johan)

    Yes! That worked, thank you so much Michael, you saved my day/week/life!

    If you would help me with this last thing I would be ever thankful, but please do not feel obliged to do so.

    The code I finished with is this, and it works like a charm in the top categories;

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
     foreach($categories as $term) {
       echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
     }
    }
    ?>

    Though the list is empty on sub-categories, I guess since it’s calling for children to the active sub-category.
    Is it possible to edit the code to work on sub-categories as well, i.e. get the same list of links while on a sub-category page as on the top category page?

    carl-johan

    (@carl-johan)

    I’m not sure if that was really clear what I just wrote, let me try to illustrate it:

    The way it works now:

    Active Category
    Sub-Category 1
    Sub-Category 2
    Sub-Category 3

    Category
    …empty…

    The way I’d like it to work:

    Active Category
    Sub-Category 1
    Sub-Category 2
    Sub-Category 3

    Parent Category
    Sub-Category 1
    Active Sub-Category 2
    Sub-Category 3

    MichaelH

    (@michaelh)

    Not exactly sure what you are after, but you can use the $term->count variable to test for count, such as:

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
     foreach($categories as $term) {
       if ($term->count >0) {
         echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
       } else {
       echo 'category with no posts';
       }
     }
    }
    ?>

    carl-johan

    (@carl-johan)

    Oh, that’s not really it.
    What I’m trying to get at is having a list of all sub-category descriptions, just as it is now on the category pages, on the sub-category pages as well.
    Say I have a category called “Illustration” and sub-categories to that called say “Freelance” and “Commissioned Work”, then I’d like the menu to look like this, while on the Freelance category page.

    Illustration

      Freelance
      Commissioned Work

    Displaying the Parent Category, as well as displaying all children of the Parent Category, not children of the active sub-category, since it has no children.

    I think what is needed is an alteration of the
    $cat = get_query_var(‘cat’);
    so that it calls for something like:
    $cat = get_query_var(‘parent_cat’);
    though that is probably not the least correct.

    I wish I could show you, but since the site is up and running, I don’t want to mess with it too much at this hour (it’s 14:45 in Sweden right now).

    MichaelH

    (@michaelh)

    In that “foreach($categories as $term) {” loop, $term->parent will contain the parent category ID.

    carl-johan

    (@carl-johan)

    That would probably solve things, but I can’t get the sub-category to use that line of code. I have this in my sidebar.php:

    <? } /* If this page displays a Parent Category */ elseif ( is_category(illustration) || is_category(graphics-design) || is_category(photo) || is_category(styling) || is_category(pattern) ) { ?>

    and tried to use just elseif ( is_category() ) for any other category (i.e. all the sub-categories) but the sub-categories still go by the first category code.

    I even tried calling the exakt sub-category ID with elseif ( is_category(’10’) ) but it still uses the top category code.

    I don’t think there is a is_subcategory but there must be another way? Or will I have to make a template for each and every sub-category?

    MichaelH

    (@michaelh)

    if ($terms->parent) {
    echo 'this is a subcategory of some kind';
    }
    carl-johan

    (@carl-johan)

    Where would that go in the code?

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
      if ($categories) {
        foreach($categories as $term) {
          echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
        }
      }
    ?>

    Sorry, but this is not my bag of crisps.

    MichaelH

    (@michaelh)

    I’ve kind of lost track of the goal here, but the $terms->parent value would be available AFTER the “foreach($categories as $term) {

    carl-johan

    (@carl-johan)

    Haha, yeah it’s been a long way, let’s see if I can sum it up, I think the way there will be clear one’s the goal is..

    You’ve helped me to list the descriptions of a category’s sub-category’s while on a category page. Each description links to that sub-category, so far it’s pretty straight forward right?

    The last issue is that ones you click on a sub-category’s description, you go to that sub-category and then the list of descriptions disappear.
    I’d like the same list of descriptions that appears on the category page to appear on the sub-category.

    Here’s the part of my sidebar.php that isn’t working, maybe it’ll make it more clear to you.

    Thanks again for taking your time!

    <div id="left_column">
    
    [...]
    
    <div id="menu">
    <ul>
    <?php /* If this page displays an Author Archive */ if ( is_author() ) { ?>
    
    <?php
    if(isset($_GET['author_name'])) :
    $curauth = get_userdatabylogin($author_name); // NOTE: 2.0 bug requires get_userdatabylogin(get_the_author_login());
    else :
    $curauth = get_userdata(intval($author));
    endif;
    ?>
    
    <h2 class="menu_header"><?php echo $curauth->display_name; ?></h2>
    <?php echo $curauth->user_email; ?>
    <?php echo $curauth->jabber; ?>
    <?php echo $curauth->description; ?>
    
    <? } /* If this page displays a Parent Category */ elseif ( is_category(illustration) || is_category(graphics-design) || is_category(photo) || is_category(styling) || is_category(pattern) ) { ?>
    
    <h2 class="menu_header">Lula Members</h2>
    
    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
    foreach($categories as $term) {
    echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
    }
    }
    ?>
    
    <? } /* If this page displays any other Category */ elseif ( is_category() ) { ?>
    
    <h2 class="menu_header">Lula Members</h2>
    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
    foreach($categories as $term->parent) {
    echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
    }
    }
    ?>
    
    <? } else { ?> 
    
    <h2 class="menu_header">Lula Members</h2>   
    
    <?php wp_list_authors('show_fullname=1&optioncount=0&exclude_admin=1'); ?>
    
    <?php } ?>
    
    [...]
    
    </ul>
    </div>
    MichaelH

    (@michaelh)

    So with this:

    .
    .
    
    <? } /* If this page displays a Parent Category */ elseif ( is_category(illustration) || is_category(graphics-design) || is_category(photo) || is_category(styling) || is_category(pattern) ) { ?>
    
    <h2 class="menu_header">Lula Members</h2>
    
    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
    foreach($categories as $term) {
    echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
    }
    }
    ?>
    
    <? } /* If this page displays any other Category */ elseif ( is_category() ) { ?>
    
    <h2 class="menu_header">Lula Members</h2>
    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
    foreach($categories as $term->parent) {
    echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
    }
    }
    ?>
    .
    .

    you are saying,

    if the category is illustration,graphics-design,photo,styling, or pattern, then display the children of these categories with the category description

    else

    ????? (this part I’m confused about)

    carl-johan

    (@carl-johan)

    I would like “else” part to say, in plain english:

    If the page displays any sub-category, then display the children of the parent category with the category description.
    I.e. display the currently active child as well as the other children of the same parent.

    Does that make any sense?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Have wp_list_categories output the category description’ is closed to new replies.