I have two functions that affect the_excerpt
:
// Grab First Paragraph Of Text And Ignore Images For Excerpt
function awesome_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
$text = preg_replace("/<img[^>]+\>/i", "", $text);
return $text;
}
add_filter( 'wp_trim_excerpt', 'awesome_excerpt', 10, 2 );
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
Now, on my index.php
, those posts show the first paragraph of the post. I am also trying to show the excerpt when you click into a posts in an area called Related Posts. In Related Posts I want it to show a limit of x characters.
On the index.php
I am leaving it as the_excerpt
, but on my Related Posts I'm calling it with <?php echo excerpt(25);?>
. This is what the markup looks like when I'm calling it for the Related Posts like:
<div class="recent-meta-excerpt">
<?php echo excerpt(25);?>
<a class="read-more" href="<?php the_permalink(); ?>">Read More <i class="fa fa-long-arrow-right"></i></a>
</div>
It's outputting like this:
<div class="recent-meta-excerpt">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout....
<a class="read-more" href="http://www.stepyouraimup.com/blog/no-featured-image/">Read More <i class="fa fa-long-arrow-right"></i></a>
</p>
</div>
But I don't want the <a>
inside. When I'm calling the_excerpt
on the Related Posts the <a>
it's inside of the <p>
. On the main posts in index.php it's wrapping the first paragraph from the post into a <p>
, this is correct. I would like to do the same for the excerpt on Related Posts, but it shouldn't have the <a>
inside of it. If it's outside of the call why is it being included inside?
If you'd like a live example click here to view my blog and click a post to see the Related Posts.