Content type related post excerpt in WordPress

in Code Write a comment

In this tutorial, you’ll learn how to change post excerpt depending on content type. Make sure you also check how to change excerpt length in WordPress.

We’ll be using the excerpt_more filter. Let’s open the functions.php and replace the default filter.

add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_more($more) {
  global $post;
  $current_post_type = get_post_type();
	
  if($current_post_type == 'case_study'):
    return '<a href="'. get_permalink($post->ID) . '"><i class="fa fa-arrow-circle-o-right"></i> Read the case study</a>';
    endif;
	
  if($current_post_type == 'post'):
    return '<a href="'. get_permalink($post->ID) . '"><i class="fa fa-arrow-circle-o-right"></i> Read more</a>';
  endif;
}

Hi, I'm Renat 👋

 


Now, if you show the post excerpt in the templates it will show different button text for different content types: case_study and post.

<?php the_excerpt(); ?>

If you find this post useful, please let me know in the comments below. 
Cheers,
Renat Galyamov

Want to share this with your friends?
👉renatello.com/wordpress-custom-post-excerpt

PS: make sure you check other WordPress tutorials, e.g. create a new sidebar in WordPressload Google Fonts via functions.php in WordPress and download my plugin that improves your WordPress SEO & Performance.

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment

Comment