• cgoldt

    (@cgoldt)


    Hey everybody,

    I would like to have a customized category list on a category page and have no idea how to do this.

    The list should ONLY show categories with posts, that are ALSO assigned to my active category (ARCHIVE).

    For example, I’m having 3 posts:
    * Apples (assigned to cat_apple)
    * Melones (assigned to cat_melone and ARCHIVE)
    * Kiwi (assigned to cat_kiwi and ARCHIVE)

    So my catlist on my category-archive template should look like this:

    melones (1)
    kiwi (1)

    Has anyobdy an idea, how to do this???

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can use conditional tags. To make sure you only show posts from a given category, you can use

    if ( in_category( 'archive' )) {
    }

    then you go on with the traditional loop, which already asks if there are posts

    Thread Starter cgoldt

    (@cgoldt)

    Hey antonietta456,

    thanks for your reply.
    You mean, instead of using <?php wp_list_categories(); ?> I should insert a custom query with conditional tags in my sidebar, that is just outputting my category names?

    Thinking again, this might be a solution:

    <?php query_posts('category_name=archive'); ?>
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li><?php the_title(); ?></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    Try it out.

    Thread Starter cgoldt

    (@cgoldt)

    This is actually not, what I need.I’m sorry, I obviouslly didn’t explain it quite clearly. I don’t want to show the titles, I would like to have category list like I get with <?php wp_list_categories(); ?>. Just with this custom filter.

    That means the catlist on my category-archive template should look like this (showing all categories, with posts, that have also the ARCHIVE category assigned to):

    cat_melone (1)
    cat_kiwi (1)

    Sorry, this seems kind of complicated ??

    If you want everything like wp_list_categories() but with the filter, the easiest solution would be this, which goes in the template you want to use to list the categories:

    <ul>
    <?php wp_list_categories('orderby=name&show_count=1&hide_empty=1&include=1'); ?>
    </ul>

    The above lists categories ordering them by name, showing the post count, that have posts and that are in the category id specified.

    Make sure you check the category ID of the archive category and replace include=1 with include=archiveid number.

    Thread Starter cgoldt

    (@cgoldt)

    Hi antonietta456,

    I tried this already and it’s unfortunately not that easy. That’s not what I’m looking for.

    Using your code I just get one category shown in the list:
    archive (2)

    But I want to show all categories that that are TOGETHER with archive assigned to posts, like:

    cat_melone (1) (there is one post assigned to archive AND cat_melone)
    cat_kiwi (1) (there is another post assigned to archive AND cat_kiwi)

    Do you know, what I mean??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_list_categories / just show categories that are also assigned to ID’ is closed to new replies.