如何从acf wysiwyg字段字符串中删除包装p标记,以便以后进行自定义摘录处理

时间:2014-02-22 13:10:28

标签: wordpress wysiwyg advanced-custom-fields

我有一个带有高级自定义字段插件的WYSIWG字段。当我用以下方式查询时:

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

输出看起来像这样:

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

但我本来希望并想要这样的东西:

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

我试图添加

$text = strip_tags ($text);

在strip_shortcodes调用查询wysiwyg自定义字段的函数之前:

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

但也没有效果。那么是否有一种方法来剥离包装p标签,同时在字符串文本体内保持可能的标签,例如链接的标签。最好的问候拉尔夫

3 个答案:

答案 0 :(得分:3)

我知道这已经老了,但也许对其他人有帮助。

我有同样的问题,使用“false,false”作为get_field的第二和第三个参数就可以了。

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

echo '<p class="custom-class">'.$myField.'</p>';

答案 1 :(得分:1)

可能是一个简单的字符串替换?

$text = get_field('title');
$stripped_text = str_replace(array('<p>','</p>'),'',$text);

答案 2 :(得分:0)

另一个选择是PHP的strip_tags函数。这将删除所有标签。

https://www.php.net/manual/en/function.strip-tags.php

$stripped_text = strip_tags( get_field( 'field_name' ) );