Add Excerpts To WordPress Main Page

I like mono-lab themes, but none of them has excerpts built-in to list posts with excerpts on the main blog page. I found some codes that actually will work with any themes that don’t have excerpts functions at all. WordPress has built-in excerpts function, but the theme has to call it out to use.

Depends upon the theme you use, on mono-lab piano-black and flat, 2 files that need to add codes to are function.php and index.php.

Function.php codes: add these line any where in the function.php. Change the return # to the maximum characters we want to display, I set mine to 100 characters.

function trim_excerpt($text) {
global $post;
return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '...' . __('Read More', 'flat') . '' . '</a>', $text);
}
add_filter('get_the_excerpt', 'trim_excerpt');

function custom_excerpt_length( $length ) {
return 100;
}
add_filter( ‘excerpt_length’, ‘custom_excerpt_length’, 999 );

Index.php, Search.php, Archive.php codes: add these lines to replace the post codes between “post_content” and “wp_link_page”

<div class="post_content">
<?php if (trim_excerpt('$text')){
the_excerpt();
} else {
the_content(__('Read more', 'flat'));
}?>
<?php wp_link_pages(); ?>

I added these codes to get excerpts on my main page of Flat theme from mono-lab

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.