11

In the code snippet below, I trying to get the_excerpt to be written out without tags. However, the source formatting shows that the_excerpt is always wrapped in P tags. How can I pull the excerpt without tags?

foreach($myrecentposts as  $idxrecent=>$post) 
{ ?>
<li class="page_item">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php echo strip_tags(substr( the_excerpt(), 0, 75 ))."..." ?>
</li><?php }    
echo "</ul>
</div>";}
13

in your code above use get_the_excerpt() instead of the_excerpt(), because the last one will output the excerpt to the screen, and not pass it to your other functions...

| improve this answer | |
7

What about removing the wpautop filter before your list?

remove_filter( 'the_excerpt', 'wpautop' );

(Make sure to add it back afterwards, so as not to mess up other formatting...)

| improve this answer | |
  • This is the correct answer and will specifically remove the formatting on the particular page that you are using to output the content. – Charles Oct 17 '16 at 16:02
0

I tried the above answers but didn't work for me.

I tried using the_excerpt but wasn't displaying any content so I used the below and it worked perfectly

// $search_text = the_excerpt();
$search_text = get_the_excerpt();

// Strip the <p> tag by replacing it empty string
$tags = array("<p>", "</p>");
$search_content = str_replace($tags, "", $search_text);

// Echo the content

echo $search_content;

I hope this throws more light for someone else too.

Cheers

| improve this answer | |
-1

Below did the trick using ACF plugin:

<p>
    <?php
        $summary = get_field('introductory_text');
        echo strip_tags(substr($summary, 0, 520));
    ?>
    <a href="<?php the_permalink(); ?>"> ...read more</a>
</p>
| improve this answer | |

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.