简体   繁体   English

WordPress - 自定义帖子类型类别的简码

[英]WordPress - Shortcode for Custom Post Type Category

Hej, i want to Display all Posts from one Category of my Custom Post Type with a Shortcode. Hej,我想使用简码显示我的自定义帖子类型的一个类别中的所有帖子。

Example:例子:

My-Custom-Post-Type: Tomatoe, Lettuce, Fruit, Vegan, Medium Rare, Rare My-Custom-Post-Type:番茄、生菜、水果、素食、中等稀有、稀有

Food-category: Burger, Pizza, Salad食物类别:汉堡、比萨、沙拉

Burger: Vegan, Medium Rare, Rare汉堡:素食主义者,中等稀有,稀有

Salad: Tomatoe, Lettuce, Fruit沙拉:西红柿、生菜、水果

Is there a way to do this?有没有办法做到这一点? Sorry for bad Example对不起例子

From your example I think you have confused CPTs with Taxonomies and Terms, but in general all you have to do is first create a custom shortcode by adding this in your functions.php:从您的示例中,我认为您已将 CPT 与分类法和术语混淆了,但通常您所要做的就是首先通过在您的函数中添加它来创建自定义简码。php

function your_custom_function ( $atts ) {

//Run your Query

//Iterate and display output

return $output;
}

add_shortcode( 'your_shortcode', 'your_custom_function' );

Then inside your function you would need a query to get and display the posts you want, example:然后在您的 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(); 

This tool can also help you with your custom query.此工具还可以帮助您进行自定义查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM