• I’m trying to figure out how to alter category.php, so that when viewing a top level category that has no posts, a list of its parent categories, along with their descriptions, are displayed.
    Any ideas?
    Thanks for any and all help!

Viewing 11 replies - 1 through 11 (of 11 total)
  • edit category.php in a child theme;

    add your new code instead of this line:

    get_template_part( 'includes/no-results', 'index' );

    https://codex.www.ads-software.com/Function_Reference/get_categories
    https://codex.www.ads-software.com/Function_Reference/category_description

    Thread Starter ashleycox

    (@ashleycox)

    Thanks – I thought it may be those functions. Get_categories requires a category ID; from where do I get that? Surely it’s not via $_GET.

    get_query_var( 'cat' ) to get the category ID.

    Thread Starter ashleycox

    (@ashleycox)

    Thanks.

    Thread Starter ashleycox

    (@ashleycox)

    Still doesn’t seem to be working; I’ve edited category.php as suggested, but viewing the category still displays a list of posts in child categories, rather than the child categories themselves. Perhaps I’m using the functions incorrectly? I’m trying to:
    If the category is a top level category, display a list of its parents.
    If the category is a child category, display its posts

    Thread Starter ashleycox

    (@ashleycox)

    Here’s category.php:

    <?php
    /**
     * @package Serene
     * @since Serene 1.0
     */
    
    get_header();
    
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		get_template_part( 'content', get_post_format() );
    	endwhile;
    
    	get_template_part( 'includes/navigation', 'index' );
    else:
    	//get_template_part( 'includes/no-results', 'index' );
    	$cat=get_query_var('cat');
    	$args = array(
    	'orderby' => 'name',
    	'parent' => $cat,
    	'order' => 'ASC'
    	);
    	$categories = get_categories($args);
    	foreach($categories as $category) {
    	echo '<p>Category: <a href="' . get_category_link( 	$category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    	echo '<p> Description:'. $category->description . '</p>';
    	}
    endif;
    
    get_footer(); ?>

    You can refer to this post.

    Can you clarify what you mean by “top-level category”? As I would use the term, a top-level category would have no parents, it could only be the parent of a child category.

    Thread Starter ashleycox

    (@ashleycox)

    Sorry; I mean a parent category. So for example, if I had the following category structure:
    reviews>speakers>floorstanding-speakers
    reviews would be the “top level” category.
    SO, if the user of my site visited the “reviews” category archive, they would see a link to “speakers”. If they visited the speakers category, they’d see a link to “florstanding-speakers”.
    Some of my parent categories have no child categories – in this case, I’d like that category archive to show the posts. However, when a parent category has children, only the lowest level categories have posts.
    Hope this makes sense ??

    Okay, check this pastebin out.

    With this code, if you click on a category page and that category has any child categories, it won’t even run the loop to display posts. Instead, it will display the names and descriptions of the child categories. If you click on a category page and that category has no child categories, it will run the loop as normal and display the posts. You can edit lines 21-22 if you’d like to change how the names and descriptions of child categories are displayed.

    Thread Starter ashleycox

    (@ashleycox)

    Thanks; It’s still not working as expected, even though only child categories have posts. Looking at the code though, it should work; so it’s probably an issue with my site, which right now I don’t have time to fix. Thanks though for the solution; I’m sure it’ll work when I have a bit more time ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Alter category.php to show sub categories?’ is closed to new replies.