Expand the most recent (last) post of a category list

Howdy Customizr developer,

The following is a quick snippet that will expand (meaning show the full content instead of the excerpt) the last published post of a category list.

As usual, I will try to show you the safest way to code this with the use of a child theme and a filter in  the functions.php file. You don’t want to modify the core, do you? 😉


Which filter?

We need first to identify what filter we want to hook our code to.

  1. Click on Customiz’it and enable the DEVELOPERS TOOLTIPS option in Dev tools section, save and go to front page.
  2. Display a category list on your website and find the tip located in any post excerpt, click on it and display the filter to use
  3. the filter to use in this case is :  ‘tc_post_list_content’


Which context?

Now we need to determine in which context we are going to filter the original function. In this example, the post content will be expanded in the following conditions :

1) We are displaying a category list : this can be check easily with the WordPress boolean is_category() .

2) We are displaying the first post of the list (meaning the last published one): this seems to be a little more tricky but in fact very simple! The global $wp_query object has a useful (but quite unknown) property : $wp_query->current_post . It simply gives the index of the loop, starting from 0. So in our case, we just want to check if this index is == 0 to expand. Cool uh?

3) We are displaying of post with no particular format (aside, image, link,…) : a standard post. When a post has no specific format defined, then the WordPress function get_post_format()  will return false .


The code

add_filter('tc_post_list_content' , 'expand_last_post' );

function expand_last_post($html) {
	//we filter only if we show a category + a standard type post + the first post of the list
	global $wp_query;
	if ( get_post_format() || !is_category() || 0 != $wp_query->current_post )
		return $html;

	global $tc_has_thumbnail;
    //get the content class
    $content_class        	= ( $tc_has_thumbnail) ? 'span8' : 'span12';

          <section class="tc-content <?php echo $content_class; ?>">

          	<?php do_action( '__before_content' ); ?>

          		<section class="entry-summary">
                    <?php the_content(); ?>
                </section><!-- .entry-summary -->

          	<?php do_action( '__after_content' ) ?>



And that should do the trick.

As always, you are more than welcome to give a feedback/url if you have implement this snippet in your website!



5 thoughts on “Expand the most recent (last) post of a category list”

Comments are closed.