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 '... — <a class="view-article" href="' . get_permalink($post->ID) . '">Continue reading</a>';
}
function custom_readmore($more) {
global $post;
return '... — <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); ?>