Excerpts and not full content, but only sometimes

WordPress filters and actions remains one of my favorite features. There is so much that you can easily manipulate without modifying a theme’s files.

For example: on my front page I like the latest published post to show the whole content with a featured image if there is one. For the rest of the articles I want to display the excerpt and no featured image. This is the same on the front page and subsequent pages.

One way to do this is to create a child theme and modify the PHP to get the effect you like. There’s a few reasons why I don’t like that option.

  1. Child themes are fun and I always recommend users create them. But I’m bad at maintaining them when the parent theme is updated.
  2. My CSS is just awful and I’m pretty sure I’d break the responsiveness of my site.
  3. Filters are cool! I can use this code in a plugin.

Filter the_content

I took a shot at making my own filter but a quick search found that Justin Tadlock had written a really good post on the topic already in 2008. I use that code in my function but added a few lines.

// Get the ID of the last published post
$mh_last_id = wp_get_recent_posts( array( 'numberposts' => '1' ,
     'post_status' => 'publish' ) )['0']['ID'];

// Get the post format. If the post is a standard format then the value will be false
$mh_post_type = get_post_format();

I do not want to show an excerpt for the latest post  and only want to modify standard post formats. Anything else I want to skip. The reason I only want to play with standard format posts is that other post types break horribly when I force the excerpt this way.

Except for adding to the conditional statement, the whole function is copied from Justin’s post. He writes great code and explains it much better than I do.

// If is the home page, an archive, or search results and not the last post and is a standard format
if((is_front_page() || is_archive() || is_search()) and $mh_last_id != get_the_ID() and !$mh_post_type )

That’s a little ugly to look at but it does the job.

What about the featured image?

In the past when I wanted to hide the featured image in a child theme I would use CSS like so.

.ryu-featured-thumbnail, .attachment-ryu-featured-thumbnail, .wp-post-image {
     display: none;
}

Which works but is a little heavy handed. My WordPress installation still sends the HTML for the featured image and the browser still pulls that image. The end user just doesn’t see it because it’s not being displayed.

I don’t want to remove featured image support from the theme and I don’t want to send that HTML either. Thanks to the post_thumbnail_html filter I don’t have to.

add_filter( 'post_thumbnail_html', 'mh_post_image_html', 10, 3 );

function mh_post_image_html( $html, $post_id, $post_image_id ) {

// Get the ID of the last published post
$mh_last_id = wp_get_recent_posts( array( 'numberposts' => '1' ,
     'post_status' => 'publish' ) )['0']['ID'];

if ( (is_front_page() || is_archive() || is_search()) and $mh_last_id != get_the_ID() ) {
     $html = '<!-- Featured image not displayed in excerpt -->';
     }
return $html;
}

This code checks if we’re the first post and replaces the post_thumbnail_html with a simple HTML comment.

Child theme or plugin?

This code works either way. At the moment I am using this in a plugin but the whole thing can be implemented in my child theme’s function.php file.

It belongs in the child theme because I’m playing with the presentation and that is theme territory. Editing a copy of content.php in the child theme directory would be more theme’ish but I like filters. Filters are cool.

I’m not good at keeping my child theme changes in sync with the parent theme so using a filter lets me just play with the results and not mess with theme files. In a plugin or the child theme’s functions.php file the filters still work the same.

There is no anonymity on the Internet

Well, at least in the U.S. there really isn’t. This morning around 2 AM EDT a troll followed me from a topic on the WordPress forums from Denver, Colorado.

He’d poked around for about 30 minutes with his Android tablet using Firefox (likely from his parents basement) and left a racist contact form submission about my wife and how I “struck out with pretty white girls and ended up in the bottom of the social barrel”.

There’s something really sad and pathetic about someone posting that from their parent’s basement after midnight their local time.

None of that bothers me; trolls on the Internet have been around forever. I’ve heard that in the work place (though that was 20 years ago) and hey, it’s the Internet.

Had he left anything remotely threatening then instead of a blog post I’d be preserving log data for law enforcement and making some calls this morning.

What irks me is when a plugin developer follows me home to continue an argument or leave a “<EXPLETIVE DELETED> YOU!” message. When that happens I don’t get angry but I do get disappointed. It doesn’t happen often as most WordPress developers are cool.

Troll are bad. WordPress developers losing their mind? More sad than anything else. I expect people who contribute to the community to be better than that. 😉

Really? Congratulations!

I get odd solicitation calls but occasionally people dial it up to eleven to try and sell things.

Yesterday Lily got a call at the store from someone looking for me. It happens and I’m pretty sure that Googling my name might lead you to the store’s WordPress site.

What was odd was that they’d pronounced my name perfectly and seem to have a Polish accent.

Lily got the phone and had this conversation.

Sales Monkey: Can I speak with Jan Dembowski?

Lily: He’s not here. Who is this and why are you calling?

Sales Monkey: Do you know when he’ll be back?

Note to Sales Monkeys everywhere? When my wife asks you a direct question just get to the point. Do not make her repeat herself. She can track down your boss, call him if she wants to and hurt you in “ways”.

Lily: Why are you calling?

Sales Monkey: Does he speak Polish and have life insurance?

Ah ha! Yep, Sales Monkey. This is the same person who called me at the house looking to speak to and sell life insurance to my late Dad. That was a fun conversation.

In short order Lily explained that we’ve got that covered and stop tracking me down. I don’t speak Polish and these are not the marks you are looking for.

Sales Monkey: (Incredulously) He does not speak Polish? He is not from Poland?

Lily: He doesn’t and he was born here in the United States.

Sales Monkey: Oh. Congratulations!

I had no idea I was special for that reason. I alway thought it was my carefully honed sense of humor.