In the Twenty Fifteen theme, the blog page displays full blog posts instead of excerpts, and there are no settings to change this. However, we can modify this behavior with a custom function.
You can achieve this with a custom function in WordPress without directly modifying the theme files. This is a more sustainable approach, especially if you’re concerned about losing changes during a theme update. You can add a custom function to your theme’s functions.php
file or in a site-specific plugin. Here’s how you can do it:
- Create a Custom Excerpt Function: Add a custom function in your theme’s
functions.php
file that modifies the output of the blog posts on your blog page. - Use Conditional Tags: Use conditional tags to check if the page being displayed is your blog page.
- Filter
the_content
: Use a filter to replacethe_content()
withthe_excerpt()
on the specific blog page.
Here’s an example of how you might write this function:
function custom_excerpt_on_blog_page( $content ) { if ( is_home() || is_archive() ) { // Checks if it's the blog page or archive page global $post; if ( has_excerpt( $post->ID ) ) { return the_excerpt(); // Return the post excerpt } else { return wp_trim_words( $content, 40, '...' ); // Trim the content to 40 words } } return $content; } add_filter( 'the_content', 'custom_excerpt_on_blog_page' );
In this example:
is_home()
checks if it’s the main blog page.is_archive()
can be included if you want the same behavior on archive pages.has_excerpt()
checks if the post has a custom excerpt.wp_trim_words()
is used to trim the content if there is no custom excerpt. You can adjust the word count as needed.- This function is hooked to
the_content
, so it will modify the content on the blog page.
This approach ensures that your changes are more likely to be preserved when you update your theme, and it keeps your theme files clean and unaltered. Remember to back up your functions.php
file before making changes.