WordPress.org

Ready to get started?Download WordPress

Forums

Have wp_list_categories output the category description (21 posts)

  1. carl-johan
    Member
    Posted 5 years ago #

    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");
    
    ?>
  2. MichaelH
    Member
    Posted 5 years ago #

    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;
      }
    }
    ?>
  3. carl-johan
    Member
    Posted 5 years ago #

    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!

  4. MichaelH
    Member
    Posted 5 years ago #

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

  5. carl-johan
    Member
    Posted 5 years ago #

    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?

  6. carl-johan
    Member
    Posted 5 years ago #

    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

  7. MichaelH
    Member
    Posted 5 years ago #

    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';
       }
     }
    }
    ?>
  8. carl-johan
    Member
    Posted 5 years ago #

    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).

  9. MichaelH
    Member
    Posted 5 years ago #

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

  10. carl-johan
    Member
    Posted 5 years ago #

    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?

  11. MichaelH
    Member
    Posted 5 years ago #

    if ($terms->parent) {
    echo 'this is a subcategory of some kind';
    }
  12. carl-johan
    Member
    Posted 5 years ago #

    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.

  13. MichaelH
    Member
    Posted 5 years ago #

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

  14. carl-johan
    Member
    Posted 5 years ago #

    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>
  15. MichaelH
    Member
    Posted 5 years ago #

    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)

  16. carl-johan
    Member
    Posted 5 years ago #

    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?

  17. MichaelH
    Member
    Posted 5 years ago #

    Okay will give this one last try for that "else"

    .
    .
    <? } /* 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) {
      $current_parent='';
      foreach($categories as $term) {
        if ($term->parent) {
          $current_parent=$term->parent;
        }
      }
      if ($current_parent) {
        $newcats=get_categories('child_of='.$current_parent);
        if ($newcats) {
          foreach($newcats as $term) {
            echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
          }
        }
      }
    }
    ?>
  18. carl-johan
    Member
    Posted 5 years ago #

    Yeah man, sorry for all your trouble. I'm so grateful that your taking time to help me. I've been working hard, testing a lot of different stuff from the code you've given.

    The last one you wrote seem to work nicely, and I could actually replaced my old is_category(yadiyada) with only your new one. Though it still won't display any children of the parent when in a sub-category. I tried putting in something like echo 'Sub-Category'; but it wouldn't return that either, though I've might put it in the wrong place.

    Anyway, I understand if you have more important issues to attend to than this, so thanks for all your help, and I'll see if I can figure this out on my own.

  19. carl-johan
    Member
    Posted 5 years ago #

    Hey again, Michael, I found this topic, basically discussing what I was looking for and found a solution too:

    <?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
    $this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
    if ($this_category) { ?>

      <?php echo $this_category; ?>

    <?php } ?>

    This code displays the right categories on the right page, though I'm back to the Category Names and not description. If you see an easy solution for that, please let me know, otherwise take care!

  20. muradh
    Member
    Posted 4 years ago #

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
    ?>
    <div class="subcat-archive">
    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
      foreach($categories as $term) {
       echo $title . '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> | ';
      }
    }
    ?>
    </div>
    <?php
    }
    else {
    ?>
    <?php
    }
    ?>

    That Does it.. for me.. i have conditional stuff.. but in between the divs is the real stuff. :D cheers!

    Murad

  21. muradh
    Member
    Posted 4 years ago #

    sorry the link is Maldives Travel Services

Topic Closed

This topic has been closed to new replies.

About this Topic