Forums

How to: If no children categories show post titles in active category (4 posts)

  1. design_dolphin
    Member
    Posted 4 years ago #

    What I would like to achieve is this:

    If no children categories exist show post titles in the active category else show children categories.

    This is a excerpt from the category.php template:

    <h1><?php
    foreach((get_the_category()) as $category) {
        echo $category->cat_name . ' ';
    }
    ?></h1>
    
    	<h2>Table of contents</h2>
    
    ***(A)***  
    
    <ul><?php echo wp_list_cats('child_of='.$cat);?>
    </ul>
    
    ***(B)*** 
    
    <?php query_posts($query_string .
    '&orderby=title&order=asc&posts_per_page=-1');?>
    	<?php if (have_posts()) : ?>
    		<ul>
    		<?php while (have_posts()) : the_post(); ?>
    			<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    		<?php endwhile; ?>
    		</ul>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.

    Where (A) and (B) in the code somehow have to be combined to a statement(?)

    Also how do I get rid of the 'no categories' statement that show when there are no categories?

    Has anyone tackled this before, or know how to do this?

    Many thanks :-)

  2. design_dolphin
    Member
    Posted 4 years ago #

    With this plugin: http://guff.szub.net/2007/11/30/kinderlose/

    Posts in children categories are not shown.

    Then the following code in category.php only shows the children categories if there are any. If no children categories it shows "no categories". Add this code before the loop.

    <?php
    $cat = $cat;
    wp_list_categories("orderby=name&show_count=1
    &use_desc_for_title=0&hide_empty=0&child_of=$cat&title_li");
    ?>

    If you leave the parent category empty of posts (which is true in my case.)
    This setup will show the children categories in the current category or if no children categories present the posts in the current category.

    This will print a 'no categories' message if the categorie is empty though. Still have to work that part out.

  3. prodaea
    Member
    Posted 3 years ago #

    I'm curious how this turned out for you. I'm trying to suppress the output of "No Categories" and replace it. Any knowledge on this would be much appreciated.

  4. design_dolphin
    Member
    Posted 3 years ago #

    @prodaea: I just read your message. Honestly I don't remember. I am not using the function anymore.

    You could possibly override the "no categories" string with an override in functions.php

Topic Closed

This topic has been closed to new replies.

About this Topic