0

wp editor was replacing br tag with p tag. not all the solutions here worked. It was causing problem with design. So i disabled wpautop with below code....

remove_filter( 'the_content', 'wpautop' );

Forgot to mentioned that excerpt disappears if used....

remove_filter( 'the_excerpt', 'wpautop' );

However, the problem still persist for excerpt. In excerpt adding single br works but two br becomes p

In order to style p of excerpt i am using below filter...

add_filter( "the_excerpt", "add_class_to_excerpt" );
function add_class_to_excerpt( $excerpt ) {
return str_replace('<p', '<p class="short-desc" style="text-align: justify;"', $excerpt);
}

At present output is as below....

<p class="short-desc" style="text-align: justify;">text here</p>
<p class="short-desc" style="text-align: justify;">text here</p>

And i want output like below....

<p class="short-desc" style="text-align: justify;">text here<br><br/>
text here</p>

How to force wp excerpt to use br tag? or how to stop wp from making two br into p tag?

4
  • Did you try remove_filter( 'the_excerpt', 'wpautop' ); ?
    – benoît
    Commented Sep 30, 2016 at 8:33
  • @benoît yes. that was the first thing i tried. That did not work.
    – NehaAgra
    Commented Sep 30, 2016 at 8:40
  • @benoît that makes excerpt disappeare.
    – NehaAgra
    Commented Sep 30, 2016 at 9:58
  • I tried using meta_box text area instead. Now i am stuck at adding <br> tag. Wordpress programming sucks.
    – NehaAgra
    Commented Sep 30, 2016 at 16:49

1 Answer 1

0

why don't you use get_the_excerpt instead. That doesn't have the paragraph marks.

You can even use your own filters. something similar.

<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>

view more on the codex

https://codex.wordpress.org/Function_Reference/get_the_excerpt

There is a the difference between get_the and just the excerpt. Returning excerpt with out "get" automatically includes the echo. It also cleans the result and adds paragraph marks. If you want to manipulate before returning you need to "get" the excerpt content, manipulate, and then echo it.

So use

<?php echo get_the_excerpt(); ?>
8
  • My work would have completed one week earlier if get_the_post was working. This <?php get_the_excerpt(); ?> is not working. Excerpt disappears.
    – NehaAgra
    Commented Oct 1, 2016 at 4:31
  • worked. But if <?php the_ecerpt(); ?> works than why <?php get_the_excerpt(); ?> is not working without echo?????
    – NehaAgra
    Commented Oct 1, 2016 at 4:58
  • that's the difference between get_the and just the excerpt. returning excerpt with out "get" automatically includes the echo. it also cleans the result and adds paragraph marks. if you want to manipulate before returning you need to "get" the excerpt content and then echo it.
    – rudtek
    Commented Oct 1, 2016 at 5:20
  • the_content is a different thing all together. it has more parameters to use when you're using it, such as your read more statement etc. if what I have worked. please mark answered. but I'll still try to help if you need more.
    – rudtek
    Commented Oct 1, 2016 at 5:22
  • No community member CARED to point that out. I started learning php 1.5 months back (self-teaching). And after that i decided to work on a project. So, i did not know so much in detail.
    – NehaAgra
    Commented Oct 1, 2016 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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