WordPress - 自定义文章类型类别的短码

问题描述 投票:-1回答:1

Hej, 我想用一个短码来显示我的自定义帖子类型的一个类别的所有帖子。

例如

My-Custom-Post-Type:Tomatoe, Lettuce, Fruit, Vegan, Medium Rare, Rare。

食品类别:汉堡、比萨、沙拉 漢堡, 比薩, 沙拉

汉堡。素食,中熟,熟食

沙拉: 番茄,生菜,水果

有什么办法吗? 对不起,不好的例子

php wordpress custom-post-type shortcode wordpress-shortcode
1个回答
0
投票

从你的例子中,我认为你已经混淆了CPTs与分类标准和术语,但在一般情况下,所有你必须做的是首先创建一个自定义的 "我"。简码 在你的functional.php中添加这个。

function your_custom_function ( $atts ) {

//Run your Query

//Iterate and display output

return $output;
}

add_shortcode( 'your_shortcode', 'your_custom_function' );

然后在你的函数里面你需要一个 疑问 来获取和显示你想要的文章,例如。

$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(); 

这个工具 也可以帮你定制查询。

© www.soinside.com 2019 - 2024. All rights reserved.