How to list Category Names in Archive (4 posts)

  1. katiebegood
    Posted 3 years ago #

    Ok, I'm sure this is basic, but I can't find the answer. When I click on a category in the sidebar of my theme, it presents me with a page that has "Archive" as the page title and then lists the post excerpts. The problem is that it doesn't give the category name as a title and unless you remember what category you clicked on, you have no idea what it is. So, how do I get "Archive" to say the category name or if not that, how do I put the Category name at the top of the list of posts?

    On another subject, how do I get rid of the "uncategorized" category in the Categories widget?

  2. Michael
    Forum Moderator
    Posted 3 years ago #


    if your theme has a category.php, integrate it there;

    if your theme only has an archive.php, integrate it in a conditional statement;

    <?php if( is_category() ) { ?>
    <h3><?php single_cat_title(); ?></h3>
    <?php } ?>

    to exclude the 'uncategorized' category from the category widget, use a filter function added to functions.php of your theme: http://www.transformationpowertools.com/wordpress/edit-widget-parameters-filter-wordpress/#category-exclude

  3. katiebegood
    Posted 3 years ago #

    Thanks, that worked perfectly. Now a related question. I have a category called "Recipes". Under that category I have child categories like "Breakfast", "Appetizers", etc. When I use your fix and click on the parent "Recipes", it says "Recipes" at the top of the page. What I would like to do then is to have the "child" titles listed before the related recipes. So, the page would be like:

    appetizer recipes
    breakfast recipes

    How can I do this?

  4. Michael
    Forum Moderator
    Posted 3 years ago #

    rough structure - without php tags and without html tags:

    in the category archive page, you can get the category id with get_query_var('cat');

    you can use that to get the child categories:

    $child_cats = get_categories('parent='.get_query_var('cat'));

    and then loop through them and get the corresponding posts:

    if( $child_cats ) foreach( $child_cats as $cats ) {
      echo $cats->name;
      $cat_posts = new WP_Query( array( 'posts_per_page' => -1, 'category__in' => array( $cats->term_id ) );
        if( $cat_posts->have_posts() ) : while( $cat_posts->have_posts() ) : $cat_posts->the_post();
        //whatever you want to output per post//
        endif; wp_rest_postdata();

    (untested - but here are links to the used functions)

    details depend on your chosen layout and html structure.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.