I use this custom excerpt from another question that lets you control the amount of paragraphs you wanted to display instead of the length of characters. It also includes "Read More" link. Upon inspecting the code, I found out that this link is surrounded by the <p>
tag. However, I wanted to add a class to this particular <p>
tag surrounding the "Read More" link.
Here's the code in question.
if ( ! function_exists( 'custom_wp_trim_excerpt' ) ) :
function custom_wp_trim_excerpt($blogo_excerpt) {
global $post;
$raw_excerpt = $blogo_excerpt;
if ( '' == $blogo_excerpt ) {
$blogo_excerpt = get_the_content('');
$blogo_excerpt = strip_shortcodes( $blogo_excerpt );
$blogo_excerpt = apply_filters('the_content', $blogo_excerpt);
// Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
$blogo_excerpt = "<p>$blogo_excerpt</p>";
$wanted_number_of_paragraph = 1;
$tmp = explode ('</p>', $blogo_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$blogo_excerpt = implode('</p>', $tmp_to_add) . '</p>';
$blogo_excerpt = str_replace(']]>', ']]>', $blogo_excerpt);
$excerpt_end = '<a href="'. esc_url( get_permalink() ) . '">Read More</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$blogo_excerpt .= $excerpt_end;
return $blogo_excerpt;
}
return apply_filters('custom_wp_trim_excerpt', $blogo_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
Additionally, I'm trying to understand some lines in this code. For example, I learned that "explode" break up the string into array. Thus, in this code, "explode" break up the whole content into each paragraph and put each paragraph in an array. Also, "implode" returns the array back into string. In other words, "implode" put each paragraph all back together to make up whole content.
Lastly, I think I might know where the code should be edited to add the class to the <p>
tag that surrounds "Read More" link
// After the content
$blogo_excerpt .= $excerpt_end;
because "$excerpt_end" is the "Read more" link and ".=" appends the "Read more" link to the post excerpt.
However, after doing some trial and errors, I am still unable to figure out how to add a class to the <p>
tag that surrounds the "Read more" link.
Please help!