【问题标题】:Shortcode meant to output custom taxonomy instead outputting the word 'array'简码旨在输出自定义分类法,而不是输出单词“数组”
【发布时间】:2021-07-22 04:57:58
【问题描述】:

尝试将自定义帖子类型分类 (property_type) 输出为简码。在它应该输出分类的那一刻,它只输出单词 Array。对 php 很陌生,所以可能是我遗漏了一些简单的东西,或者完全找错了树。

代码是:

function prop_type_shortcode() { 

 $terms = wp_get_post_terms($post->ID, 'property_type');
    if ($terms) {
        $out = array();
        foreach ($terms as $term) {
            $out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'property_type') .'">' .$term->name .'</a>';
        }
        echo join( ', ', $out );
    } 
    
 

return $terms;
} 

add_shortcode('type', 'prop_type_shortcode');

提前感谢您的帮助。

【问题讨论】:

    标签: php wordpress shortcode custom-taxonomy


    【解决方案1】:

    您正在从wp_get_post_terms() 返回数组。相反,您应该返回已处理的文本。

    function prop_type_shortcode() {
        global $post;
    
        $terms = wp_get_post_terms($post->ID, 'property_type');
    
        if (!$terms) {
            return '';
        }
    
         $out = array();
    
         foreach ($terms as $term) {
            $out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'property_type') .'">' .$term->name .'</a>';
         }
    
        return join( ', ', $out );
    } 
    

    【讨论】:

    • 感谢分享。我试过了,现在根本没有输出。通过它,看看我是否能弄清楚为什么但被卡住了。
    • 忘记声明全局 $post 对象。
    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2018-01-25
    • 2015-01-15
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多