4

我有一个带有高级自定义字段插件的 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 调用查询所见即所得自定义字段的函数之前:

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 标签,同时将可能的标签保留在字符串文本主体内,例如链接标签。最好的问候拉尔夫

4

4 回答 4

7

我知道这是旧的,但也许它可以帮助别人。

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

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

echo '<p class="custom-class">'.$myField.'</p>';
于 2017-02-24T10:14:26.620 回答
3

也许是一个简单的字符串替换?

$text = get_field('title');
$stripped_text = str_replace(array('<p>','</p>'),'',$text);
于 2014-05-15T16:05:04.113 回答
0
remove_filter ('acf_the_content', 'wpautop');
于 2022-02-06T08:17:19.790 回答
0

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

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

$stripped_text = strip_tags( get_field( 'field_name' ) );
于 2020-11-25T17:11:08.413 回答