去除产品元数据中的 HTML

我为 Woo store 创建了一个功能来显示自定义分类法。不知何故,我的每一个 span 容器都被破坏了。这是代码:


add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );

function add_my_meta() {

    $series = the_terms($post->ID, 'series');

    if ($series) {

        $meta_output = '<span style="display:block;">Series: ';

        $meta_array = array();

        foreach ($series as $serie) {

            $meta_array[] = '<a href="' . get_term_link( $serie->slug, 'series') .'">' . $serie->name . '</a>';

        }

        $meta_output .= join( ', ', $meta_array ) . '</span>';

    }

    return $meta_output;

}

所以预期的输出是<span style="display:block;">Series: <a href="https://example.com/series/myseries/" rel="tag">My Series</a></span>


当前输出是<a href="https://example.com/series/myseries/" rel="tag">My Series</a>


跨度和文本已删除。以前从未遇到过这个问题,问题是什么以及如何解决?


慕侠2389804
浏览 61回答 1
1回答

翻过高山走不出你

发现了一些错误——需要使用 get_the_terms(是 the_terms)和 $serie->term_id(是 $serie->slug)add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );function add_my_meta() {&nbsp; &nbsp; $series = get_the_terms($post->ID, 'series');&nbsp; &nbsp; if ( is_array($series) ) {&nbsp; &nbsp; &nbsp; &nbsp; $meta_array = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($series as $serie) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $meta_array[] = '<a href="' . get_term_link( $serie->term_id, 'series') .'">' . $serie->name . '</a>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '<span class="tagged_as">Series: ' . implode( ', ', $meta_array ) . '</span>';&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP