I'm trying to add this custom excerpt to blog posts, so that it grabs the first 35 characters from Posts and makes that the excerpt. However the code works for all Post Types, even custom post types.
How do I only have this custom excerpt apply only to blog posts? I've tried to wrap the code below in the following conditional - which is what the default Blog Post page is set to - but didn't work.
if(is_page_template('template-blog-list.php')) {
function custom_excerpts($content = false) {
global $post;
$content = wp_strip_all_tags($post->post_content);
$excerpt_length = 35;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...');
$content = implode(' ', $words);
endif;
$content = $content . '<br><br><a class="more-button" href="'. get_permalink($post->ID) . '">Read More</a>';
return $content;
}
add_filter('get_the_excerpt', 'custom_excerpts');
}
You can just check the $post->post_type
:
if ( $post->post_type !== 'post' ) {
return $content;
}
Add this line after global $post
line.