0

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.

-1

Depending on where in the loop you call excerpt() wpautop might be creating the <p>...</p> wrapper. In that case, you could wrap $excerpt with <p>...</p> before returning it.

Edit: Try this approach <?php echo excerpt(25)."\n";?> (or possibly "\n\n") to change the behavior.

in function awesome_excerpt() you have $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 ); So, $text will have to contain a beginning <p>. That is likely causing what you describe. Add str_ireplace('<p>', '', $excerpt) in function excerpt() before calling explode.

In the current code shown in your question it's unclear where function excerpt() is being called.

Why not just use the excerpt_length filter to modify the length of the excerpt? From the Codex:

function custom_excerpt_length( $length ) { return 25; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

  • I tried using what was in the codex, but I only want the excerpt to show x characters in certain areas, that being wherever I call the excerpt when I'm getting Recent Posts. On the main blog, index.php, I want it to pull the first paragraph of the post. – Darren Bachan Jun 6 '16 at 2:09
  • So, what happens if you remove the starting <p> from $text? – BillK Jun 6 '16 at 6:35
  • not entirely sure how to do that, I grabbed both functions from two separate tutorials. My php skills are super weak to see where that <p> is. I only see I can edit the closing tag. – Darren Bachan Jun 6 '16 at 7:34
  • 1
    Well, the preferred way would be to use a debugger. Without that the hardcore way is to add echo $text; die; right after $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );. The value of $text will be displayed and WP stops. Copy the value from the screen and store it somewhere then delete echo $text; die; – BillK Jun 7 '16 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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