简码旨在输出自定义分类法,而不是输出单词'array'



尝试将自定义post-type分类法(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 );
} 

最新更新