wp_get_post_categories() 和get_category() 专门针对category 分类学您正在使用自定义分类法,portfolio_cat, 所以你需要使用get_the_terms() 获取类别。
$categories = get_the_terms( $post_id, \'portfolio_cat\' );
$cat_str = \'\';
foreach ( $post_categories as $category ) {
$cat_str .= $category->name;
}
如果您只需要以逗号分隔的类别链接列表,可以使用
get_the_term_list() 要简化代码,请执行以下操作:
$cat_str = get_the_term_list( $post_id, \'portfolio_cat\', \'\', \', \', \'\' );
或者,如果您不需要链接,可以这样做:
$categories = get_the_terms( $post_id, \'portfolio_cat\' );
$cat_str = implode( \', \', wp_list_pluck( $categories, \'name\' ) );
此外,代替
$query->post->post_title 你真的应该
get_the_title();.