Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove wrapping p tags from acf wysiwyg field string for later custom excerpt processing

I have a WYSIWG field with the advanced custom fields plugin. When i query it with :

<p class="bran-hn news-excerpt"><?php echo custom_field_excerpt('news_post'); ?></p>

the output looks like that:

<p class="bran-hn news-excerpt"></p>
<p>Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>
<p></p>

But i would have expected and want something like:

<p class="bran-hn news-excerpt">Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>

i tried to add

$text = strip_tags ($text);

right before the strip_shortcodes call into the function querying the wysiwyg custom field:

function custom_field_excerpt($title) {
    global $post;
    $text = get_field($title); 
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = 35; // 20 words
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('the_excerpt', $text);
}

but also no effect. so is there a way to strip the wrapping p tags while keeping possible tags inside the strings text body like e.g. a tags for links. Best regards Ralf

like image 317
rkoller Avatar asked Dec 25 '22 13:12

rkoller


2 Answers

I know this is old but maybe it helps someone else.

I had the same issue and using "false, false" as second and third parameter for get_field did the trick.

$myField = get_field('field_name', false, false);

echo '<p class="custom-class">'.$myField.'</p>';
like image 135
A. D'Alfonso Avatar answered Dec 28 '22 08:12

A. D'Alfonso


Maybe a simple string replace?

$text = get_field('title');
$stripped_text = str_replace(array('<p>','</p>'),'',$text);
like image 24
rop Avatar answered Dec 28 '22 06:12

rop