wordpress 允许在商店页面上的产品简短描述中使用内部HTML标记

lyr7nygr  于 2023-03-01  发布在  WordPress
关注(0)|答案(1)|浏览(103)

我在首页上显示产品的简短描述。我的问题是,html标签,例如列表,没有显示。文本没有任何html。在我的单一产品页面上,一切都显示良好。有办法保留这些标签吗?

e0bqpujr

e0bqpujr1#

WordPress默认情况下不显示HTML摘录。我们需要创建一个函数,以不同的方式过滤内容,WordPress原生地做它,我们取代过滤器,使用我们的,而不是WordPress的一个。
这是解决方案:

function lt_html_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
        /*just add all the tags you want to appear in the excerpt --
        be sure there are no white spaces in the string of allowed tags */
        $text = strip_tags($text,'<p><br><b><a><em><strong>');
        /* you can also change the length of the excerpt here, if you want */
        $excerpt_length = 55; 
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
    }
    return $text;
}

然后,我们删除默认过滤器,并添加您使用上面的函数创建的过滤器:

/* remove the default filter */
remove_filter('get_the_excerpt', 'wp_trim_excerpt');

/* now, add your own filter */
add_filter('get_the_excerpt', 'lt_html_excerpt');

已找到解决方案here

相关问题