我正在尝试将我的“portfolio”自定义post类型的当前post类别输出为一个短代码。
现在,我在functions.php中有了这个片段
function shows_cats(){
$page_id = get_queried_object_id();
echo 'Categories';
echo get_the_category_list('','',$page_id);
}
add_shortcode('showscats','shows_cats');
我试过几种不同的组合,叫CPT,但没有运气。我意识到这个片段只是输出标准WP类别,但我想知道是否有人知道如何编辑这个来显示CTP。
非常感谢你的帮助
发布于 2021-01-11 17:12:38
你可以试试这样的方法:
// Get the taxonomy's terms
$terms = get_terms(
array(
'taxonomy' => 'your-taxonomy',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) { ?>
name; ?>
发布于 2021-01-12 21:27:16
谢谢你的回复。
我对此进行了另一次研究,得到了一些作为短代码工作但并不完美的东西。将以下内容添加到我的functions.php文件中,并使用WPBakery中的短代码,它将当前的自定义post类型分类法作为列表输出。我的CPT分类法是“项目型”。
唯一的问题是,它显示了属于CPT的所有类别,而不是应用于当前帖子的类别。
// First we create a function
function shows_cats( $atts ) {
// Inside the function we extract custom taxonomy parameter of our shortcode
extract( shortcode_atts( array(
'custom_taxonomy' => 'project-type',
), $atts ) );
// arguments for function wp_list_categories
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
// We wrap it in unordered list
echo '';
echo wp_list_categories($args);
echo '';
}
// Add a shortcode that executes our function
add_shortcode( 'showscats', 'shows_cats' );
https://wordpress.stackexchange.com/questions/381292
复制相似问题