【发布时间】:2020-04-30 09:18:16
【问题描述】:
Hej,我想使用简码显示我的自定义帖子类型的一个类别中的所有帖子。
例子:
我的自定义帖子类型: 番茄、生菜、水果、素食、中等稀有、稀有
食品类: 汉堡、披萨、沙拉
汉堡: 素食主义者,中等稀有,稀有
沙拉: 西红柿、生菜、水果
有没有办法做到这一点? 对不起,不好的例子
【问题讨论】:
标签: php wordpress custom-post-type shortcode wordpress-shortcode
Hej,我想使用简码显示我的自定义帖子类型的一个类别中的所有帖子。
例子:
我的自定义帖子类型: 番茄、生菜、水果、素食、中等稀有、稀有
食品类: 汉堡、披萨、沙拉
汉堡: 素食主义者,中等稀有,稀有
沙拉: 西红柿、生菜、水果
有没有办法做到这一点? 对不起,不好的例子
【问题讨论】:
标签: php wordpress custom-post-type shortcode wordpress-shortcode
从您的示例中,我认为您将 CPT 与分类法和术语混淆了,但通常您所要做的就是首先通过在您的 functions.php 中添加这个来创建自定义 shortcode:
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 还可以帮助您进行自定义查询。
【讨论】: