WordPress - 自定义 Post 类型类别的简码
WordPress - Shortcode for Custom Post Type Category
Hej,我想显示来自我的自定义 Post 类型的一个类别的所有 Post 短代码。
示例:
我的-自定义-Post-类型:
西红柿、生菜、水果、素食、三分熟、稀有
食品类别:
汉堡、披萨、沙拉
汉堡:
素食主义者,三分熟,稀有
沙拉:
番茄、生菜、水果
有办法吗?
对不起,不好的例子
根据您的示例,我认为您将 CPT 与分类法和术语混淆了,但一般来说,您所要做的就是首先创建一个自定义 shortcode,方法是将其添加到您的 functions.php 中:
function your_custom_function ( $atts ) {
//Run your Query
//Iterate and display output
return $output;
}
add_shortcode( 'your_shortcode', 'your_custom_function' );
然后在你的函数中你需要一个 query 来获取和显示你想要的帖子,例如:
$args = array(
'post_type' => 'my_custom_post_type',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'cat' => 3,
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li'> . the_title() . '</li>';
endwhile;
wp_reset_postdata();
This tool 也可以帮助您进行自定义查询。
Hej,我想显示来自我的自定义 Post 类型的一个类别的所有 Post 短代码。
示例:
我的-自定义-Post-类型: 西红柿、生菜、水果、素食、三分熟、稀有
食品类别: 汉堡、披萨、沙拉
汉堡: 素食主义者,三分熟,稀有
沙拉: 番茄、生菜、水果
有办法吗? 对不起,不好的例子
根据您的示例,我认为您将 CPT 与分类法和术语混淆了,但一般来说,您所要做的就是首先创建一个自定义 shortcode,方法是将其添加到您的 functions.php 中:
function your_custom_function ( $atts ) {
//Run your Query
//Iterate and display output
return $output;
}
add_shortcode( 'your_shortcode', 'your_custom_function' );
然后在你的函数中你需要一个 query 来获取和显示你想要的帖子,例如:
$args = array(
'post_type' => 'my_custom_post_type',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'cat' => 3,
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li'> . the_title() . '</li>';
endwhile;
wp_reset_postdata();
This tool 也可以帮助您进行自定义查询。