用于输出自定义分类法而不是输出单词 'array' 的简码

Shortcode meant to output custom taxonomy instead outputting the word 'array'

正在尝试将自定义 post 类型分类法 (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');

在此先感谢您的帮助。

您正在从 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 );
}