What I want

I need to display different types of excerpts. Some items need to have one type of "read more" button, others need to have another type of it. The same goes to the length of the excerpts.

The problem I have

Right now the full excerpt is shown on both types of excerpts and the read more button is totally missing.

The code

Custom excerpt length functions:

function custom_excerpt_long($length) {
   return 100;

function custom_excerpt_short($length) {
   return 30;

Custom excerpt "read more" button functions:

function custom_continuereading($more) {
   global $post;
   return '... &mdash; <a class="view-article" href="' . get_permalink($post->ID) . '">Continue reading</a>';
function custom_readmore($more) {
   global $post;
   return '... &mdash; <a class="view-article" href="' . get_permalink($post->ID) . '">Read more</a>';

Custom excerpt callback function:

function custom_excerpt($length_callback = '', $more_callback = '') {
   global $post;

   if (function_exists($length_callback)) {
      add_filter('excerpt_length', $length_callback);
   if (function_exists($more_callback)) {
      add_filter('excerpt_more', $more_callback);

   $output = get_the_excerpt();
   $output = apply_filters('wptexturize', $output);
   $output = apply_filters('convert_chars', $output);
   $output = '<p>' . $output . '</p>';
   return $output;

Code to print the excerpt:

<?php echo custom_excerpt('custom_excerpt_long', 'custom_continuereading'); ?>
<?php echo custom_excerpt('custom_excerpt_short', 'custom_readmore'); ?>

What am I doing wrong? Thank you all for your help!

UPDATE - Working solution

With the answer of D.Dan I went for another solution. This is the final function:

function custom_excerpt($length = '') {
   global $post;

   $output = get_the_excerpt();
   $output = apply_filters('wptexturize', $output);
   $output = apply_filters('convert_chars', $output);
   $output = substr($output, 0, $length);
   $output = '<p>' . $output . '... <br><br> <a class="view-article" href="' . get_permalink($post->ID) . '">Continue reading</a></p>';
   return $output;

And I can call the function like this:

<?php echo custom_excerpt(100); ?>

1 Answer 1


Why not use substr?

Function example that returns shortened excerpt:

function shortened_excerpt() {

    echo substr(get_the_excerpt(), 0, 30) . "...";

  • 1
    It worked yesterday with my code. So I really wanted to know what's wrong with my code. But your answer definitly works. I'll post my code that worked and accept your answer. Thank you!
    – ismaelw
    Jan 5, 2018 at 13:31
  • I didn't bother looking too deep into your code, because I saw a simpler way of doing it, and suggested that.
    – D. Dan
    Jan 8, 2018 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.