简体   繁体   English

从自定义帖子类型 Wordpress 的类别中获取帖子

[英]Getting posts from category of custom post type Wordpress

I have the code below, used to to create a category.我有下面的代码,用于创建一个类别。 How can i get posts using this category ?我如何获得使用此类别的帖子? I can get the id of this category, but can`t get all the posts of the category.我可以得到这个分类的id,但是不能得到这个分类的所有帖子。

function create_taxonomy()
{
    $labels= array(
    'name' => 'Kategórie',
    'singular_name' => 'Kategórie',
    'search_items' => 'Vyhladať kategórie',
    'all_items' => 'Všetky kategórie',
    'edit_item' => 'Upraviť',
    'update_item' => 'Aktualizovať',
    'add_new_item' => 'Pridať',
    'new_item_name' => 'Nový názov',
    'menu_name' => 'Kategórie',
    );
    register_taxonomy('Categories', array('service'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'categories')
    ));
}
add_action('init','create_taxonomy',0);

here is post type这是帖子类型

https://pastebin.com/0eAPeBMr https://pastebin.com/0eAPeBMr

The below code should do the trick.下面的代码应该可以解决问题。

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'categories',
            'field' => 'slug', //can be set to ID
            'terms' => 'category-slug' //if field is ID you can reference by cat/term number
        )
    )
);
$query = new WP_Query( $args );

Set the ' taxonomy ' to your category taxonomy and change the terms to your category-slug.将“分类法”设置为您的类别分类法,并将术语更改为您的分类号。 If you want to do it by Category ID, then change the field value to "term_id" and provide the category ID in terms value.如果您想按类别 ID 执行此操作,请将字段值更改为“term_id”并在条款值中提供类别 ID。

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

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