WordPress.org

Forums

Display name of child category beside excerpt (6 posts)

  1. trmash
    Member
    Posted 4 years ago #

    Hello,

    I am having trouble figuring out how to display the child category of the current category next to the post excerpt.

    For example, if I have a post that is listed under 'Sport', and then 'Fishing' (a subcategory of Sport) then I would like my sport category page to display 'Fishing' next to the post excerpt.

    If I use <?php query_posts('cat=6'.'&orderby=date&order=desc&showposts=20'); ?> and then <?php single_cat_title(); ?> then the category title shows the current name ('Sport'), not the sub-category.

    I'm trying to display this via a page template, so it's not part of the loop.

    <?php query_posts('cat=6'.'&orderby=date&order=desc&showposts=20'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
    <?php the_time('F j Y') ?>
    <?php the_title(); ?>
    <?php single_cat_title(); ?> | <?php the_excerpt(); ?>
    <?php endwhile; ?>

    Any help appreciated!

  2. Digital Raindrops
    Member
    Posted 4 years ago #

    The post could belong to a number of categories so if you wanted just the first?

    This is UNTESTED code.

    < php $post_cats = wp_get_post_categories( $post->ID ); ?>
    <?php if($post_cats) : ?>
    <?php echo $post_cats[0]->name ; ?> | <?php the_excerpt(); ?>
    <?php else: ?>
    <?php the_excerpt(); >
    <?php endif; ?>

    EDITED to deal with same category UNTESTED:

    <?php $catid=6;
    <?php query_posts('cat='.$catid.'&orderby=date&order=desc&showposts=20'); ?>
    <?php while (have_posts()) : the_post(); ?>
    	<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
    	<?php the_time('F j Y') ?>
    	<?php the_title(); ?>
    	<?php
    	$cat_name='';
    	$post_cats = wp_get_post_categories( $post->ID );
    	foreach($post_cats as $c){
    		if $c->cat_ID != $catid {
    			$cat_name = $c->cat_name;
    			break;
    		}
    	}
    	?>
    	<?php if($cat_name) : ?>
    		<?php echo $cat_name ; ?> | <?php the_excerpt(); ?>
    	<?php else: ?>
    		<?php the_excerpt(); >
    <?php endwhile; ?>

    HTH

    David

  3. trmash
    Member
    Posted 4 years ago #

    Thanks David.

    Unfortunately there seems to be an issue with the syntax in

    if $c->cat_ID != $catid {
          $cat_name = $c->cat_name;
          break;
       }
    }

    Appreciating your help!

  4. trmash
    Member
    Posted 4 years ago #

    Not sure if it's perfect code, but this seems to be working:

    <?php $categories = get_categories("child_of=6"); foreach ($categories as $cat) { ?>
       <?php query_posts("cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name"); ?>
       <?php while (have_posts()) : the_post(); ?>
       <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
     <a href="<?php  echo get_category_link($cat->cat_ID) ?>"><?php single_cat_title(); ?></a> | <?php the_excerpt(); ?>
       <?php endwhile; ?>
    <?php }?>
  5. Digital Raindrops
    Member
    Posted 4 years ago #

    Missing Parentheses ()

    if( $c->cat_ID != $catid ){
          $cat_name = $c->cat_name;
          break;
       }
    }

    David

  6. trmash
    Member
    Posted 4 years ago #

    Thanks David, that's better.

    One more thing though - this orders the posts by sub-category, then date. Is it possible to just have it ordered by date? As in, most recent posts first regardless of sub-category.

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic