WordPress.org

Ready to get started?Download WordPress

Forums

Serene
Alter category.php to show sub categories? (12 posts)

  1. ashleycox
    Member
    Posted 3 months ago #

    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!

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 months ago #

    edit category.php in a child theme;

    add your new code instead of this line:

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

    http://codex.wordpress.org/Function_Reference/get_categories
    http://codex.wordpress.org/Function_Reference/category_description

  3. ashleycox
    Member
    Posted 3 months ago #

    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.

  4. stephencottontail
    Member
    Posted 3 months ago #

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

  5. ashleycox
    Member
    Posted 2 months ago #

    Thanks.

  6. ashleycox
    Member
    Posted 2 months ago #

    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

  7. ashleycox
    Member
    Posted 2 months ago #

    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(); ?>
  8. leapoahead
    Member
    Posted 2 months ago #

    You can refer to this post.

  9. stephencottontail
    Member
    Posted 2 months ago #

    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.

  10. ashleycox
    Member
    Posted 2 months ago #

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

  11. stephencottontail
    Member
    Posted 2 months ago #

    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.

  12. ashleycox
    Member
    Posted 2 months ago #

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

Reply

You must log in to post.

About this Theme

About this Topic