A Remaining Problem with Title-less Posts
When showing title-less posts in the archives, I would prefer to show the first few words of the post rather than nothing. Is there an easy way to do that?
**UPDATE 2018-07-15:** I ended up making a child theme and hooking into the simple-yearly-archive plugin, which I’m using for the archives page.
The only catch is that the plugin doesn’t include the post content column in the query so I had to edit the actual plugin query to include the column and also pass the entire $post object in the sya_the_title hook. I’ll have to watch out for updates, as they’ll overwrite this. Or I’ll just need to fork it.
add_filter( 'sya_the_title', 'my_sya_filter_title', 10, 2 );
function my_sya_filter_title( $title, $post ) {
return $title == '' ? jab_truncate(strip_tags($post->post_content)) : $title;
}
function jab_truncate($string,$length=50,$append="…") {
$string = trim($string);
// break at word
if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n", $string, 2);
$string = $string[0] . $append;
}
return $string;
}