WordPress.org

Ready to get started?Download WordPress

Forums

Display child categories and if there are none display current posts (8 posts)

  1. zelimir83
    Member
    Posted 3 years ago #

    Hi, Im having a problem displaying child categories. Ive tried everything, I can get child categories displaying, but when I open them it says "No categories". What I want to do is, if there are child categories, display them, if there are none, display a post from the current category. This seems next to impossible, Ive searched a bunch of forums, a lot of guys have this problem, but it seems theres no real solution. I`ve gotten this far:

    <?php
    
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('orderby=id&title_li=&child_of=' . $cat_id);
    
    	if ($catlist) echo $catlist; else { ?>
    
    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
      	<div class="archive">
        	<div class="thumb left">
        		<a>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&h=170&w=250&" alt="<?php the_title(); ?>" /></a>
        	</div> <!--end: thumb-->
          	<h2><a>" rel="bookmark"><?php the_title(); ?></a></h2>
          		<?php the_advanced_excerpt('length=160&use_words=0'); ?>
    	<strong><h4><a>" rel="bookmark">[Čitaj dalje...]</a></h4></strong>
        	<div class="clear"></div>
      </div> <!--end: archive-->
      <?php endwhile; ?>
      	<div class="clear"></div>
      	<div class="pagenavi">
      		<div class="nextprev left">
        		<?php previous_posts_link('« Raniji članci ') ?>
        	</div>
        	<div class="nextprev right">
          		<?php next_posts_link(' Noviji članci »') ?>
        	</div>
        <div class="clear"></div>
    	</div> <!--end: pagenavi-->
    	<?php else : ?>
        <?php endif; ?>
    	<?php }; ?>

    But I cannot seem to get the posts displayed, all I get is "No categories" message. Please help! Thanks!

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

    to get the 'wp_list_categories()' into a variable, you need to use the 'echo' parameter and set it to 'false'

    http://codex.wordpress.org/Template_Tags/wp_list_categories

  3. zelimir83
    Member
    Posted 3 years ago #

    Nope, the echo is not the problem. Just put it in, nothing happened.

    Do you understand the problem? I get the child category list printed, but when WordPress needs to display an archive or category page (archive.php or category.php) where there are no categories, or you`ve clicked the parent category which has no child categories any more, with the code above all you get is the "No categories" printed, and I need posts from the current category printed.
    This is what the code looks like now:

    <?php // list child categories
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id); ?>
    
    			<?php if ($catlist) {
    echo $catlist;
    } else {
    include(TEMPLATEPATH . '/includes/entry.php');
    } ?>

    Entry.php contains standard archive.php code, where all of the posts from a certain category and its child categories get displayed. I cannot seem to get the else condition to work when you switch to a post where there are no categories any more. This means if my parent category is "Solar system", and it has a child category "Moon", when I click on the "Moon", I need the posts from the "Moon" category displayed, but I get "No categories" with the code above.

  4. zelimir83
    Member
    Posted 3 years ago #

    Also, this is the Entry.php code:

    [code moderated as per forum rules - please use the pastebin]

  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    you are testing something ($catlist) which is never an empty string.

    try to change this:

    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id); ?>

    to this:

    $catlist = wp_list_categories('echo=0&show_option_none=&orderby=id&title_li=&child_of=' . $cat_id); ?>

    (untested)

    if this does not work, you may have to change your test from:
    <?php if ($catlist) {
    to:
    <?php if (get_categories('parent=' . $cat_id)) {

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

  6. zelimir83
    Member
    Posted 3 years ago #

    Hi, thanks a lot! You were right, all I needed was to use a different method of testing the variable $catlist, this is the code now:

    <?php // list child categories
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id); ?>
    
    			<div class="cat-items"><ul><li><?php if (get_categories('parent=' . $cat_id)) {
    echo $catlist; }
     else {  ?></li></ul></div>
    <?php include(TEMPLATEPATH . '/includes/entry.php'); ?>
    	<?php } ?>

    The only problem now is that I need to wrap up the list of categories in a div of class "cat-items", and I seem to be wrapping it wrongly. If I wrap up the entire php block, of course, I get the normal posts formatted as the list, and I only need to format the echo part, any idea on how I can do this, how can I wrap up just the echo $catlist part of the code into a div and li tags?

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    try:

    <?php // list child categories
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id); ?>
    
    <?php if (get_categories('parent=' . $cat_id)) { ?>
    
    <div class="cat-items"><ul>
    <?echo $catlist; ?>
    </ul></div>
    
    <?php } else {  ?>
    <?php include(TEMPLATEPATH . '/includes/entry.php'); ?>
    	<?php } ?>

    (i removed the <li> as they would result in invalid html code.)

  8. zelimir83
    Member
    Posted 3 years ago #

    Nevermind, I got it. If anyone needs it, here`s the solution:

    <?php // list child categories
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id); ?>
    
    			<?php if (get_categories('parent=' . $cat_id)) {
    echo '<div class="cat-items"><ul><li>' . $catlist . '</li></ul></div>'; } 
    
     else {  ?>
    <?php include(TEMPLATEPATH . '/includes/entry.php'); ?>
    	<?php } ?>

    alchymyth thanks a lot for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic