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.
- Click on Customiz’it and enable the DEVELOPERS TOOLTIPS option in Dev tools section, save and go to front page.
- 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
- 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' ) ?> </section> <?php }
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”
hi
could you also provide the amended code to expand ALL posts with a tag ‘xyz’
thx
Never mind. Solved. Two things:
The function names don’t match
‘expand_most_recent_post’ should be ‘expand_last_post’, yes?
I also stripped out all of the conditionals except for
and it works now.
Hi Esther, congratulations for finding the answer yourself!
And thanks for the correction : you are right, the filter callback and the function did not match, it is fixed now.
Bests!
Cool. Hey, while we’re on the subject — I looked thru the FAQ but couldn’t find the answer — is there a way to also include the comments form after said expanded current post? Basically what I want to achieve is: “Front page” contains current post, expanded, with comments; sidebar only lists posts after that (or current post in sidebar link is disabled, since it’s on the front page). Thanks!
Hi Nicolas,
Thanks for the Bootstrap-based theme. I’m fairly new to WordPress but reasonably adept with HTML5/CSS3. I have had a fairly easy time styling Customizr using a Child Theme to match my brand, but I’m dreadful when it comes to sussing out PHP.
I tried following your instructions (which got muddy right around trying to figure out which filter (I tried the “functions.php” and then the “tc_post_list_content” function inside of “class-content-post_list.php” but then my posts failed to render at all. Also not clear whether to copy into my child theme or modify and replace, and how/where to do all this.
Please help. I’ve spent several hours on this and must concede defeat.