Skip to main content
added 2 characters in body
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

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:

On the index.php I am leaving it as the_excerpt, but on my Related Posts I'm calling it with . This is what the markup looks like when I'm calling it for the Related Posts like:

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:

better title and explanation
Source Link
Darren Bachan
  • 375
  • 2
  • 7
  • 17

Custom Merging two excerpt function printing <a> text inside a <p> when it shouldn'tfunctions to work with conditions

I may need to work on the titling of this.

I have a functiontwo 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;
    }

AndNow, 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 . This is what my HTMLthe markup looks like when I'm calling it for the Related Posts like:

But I don't want the <a> inside. When I'm calling the_excerpt on the excerptRelated Posts the <a> isn't even inside so I'm not sure how it's being wrapped in ainside of the <p>.

EDIT

I realized I didn't include a crucial function On the main posts in index. After removing it,php it's wrapping the first paragraph from the post into a <p> disappeared.

// 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 );

I'm using, this so thatis correct. I would like to do the same for the excerpt on my main blogRelated Posts, but it shouldn't have the (index.php<a>) inside of it pulls the first paragraph. If it's outside of the article. When youcall why is it being included inside?

If you'd like a live example click on an article youhere to view my blog and click a post to see the Related Posts, this is where I want to limit the character count.

Custom excerpt function printing <a> text inside a <p> when it shouldn't

I may need to work on the titling of this.

I have a function:

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;
    }

And I'm calling it with . This is what my HTML looks like:

But I don't want the <a> inside. When I'm calling the excerpt the <a> isn't even inside so I'm not sure how it's being wrapped in a <p>.

EDIT

I realized I didn't include a crucial function. After removing it, the <p> disappeared.

// 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 );

I'm using this so that on my main blog (index.php) it pulls the first paragraph of the article. When you click on an article you see Related Posts, this is where I want to limit the character count.

Merging two excerpt functions to work with conditions

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 . This is what the markup looks like when I'm calling it for the Related Posts like:

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.

added 751 characters in body
Source Link
Darren Bachan
  • 375
  • 2
  • 7
  • 17

I may need to work on the titling of this.

I have a function:

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;
    }

And I'm calling it with . This is what my HTML looks 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 the <a> isn't even inside so I'm not sure how it's being wrapped in a <p>.

EDIT

I realized I didn't include a crucial function. After removing it, the <p> disappeared.

// 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 );

I'm using this so that on my main blog (index.php) it pulls the first paragraph of the article. When you click on an article you see Related Posts, this is where I want to limit the character count.

I may need to work on the titling of this.

I have a function:

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;
    }

And I'm calling it with . This is what my HTML looks 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 the <a> isn't even inside so I'm not sure how it's being wrapped in a <p>.

I may need to work on the titling of this.

I have a function:

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;
    }

And I'm calling it with . This is what my HTML looks 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 the <a> isn't even inside so I'm not sure how it's being wrapped in a <p>.

EDIT

I realized I didn't include a crucial function. After removing it, the <p> disappeared.

// 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 );

I'm using this so that on my main blog (index.php) it pulls the first paragraph of the article. When you click on an article you see Related Posts, this is where I want to limit the character count.

Source Link
Darren Bachan
  • 375
  • 2
  • 7
  • 17
Loading