简体   繁体   English

Wordpress 不显示我的自定义帖子类型帖子

[英]Wordpress does not display my custom post type posts

I created a custom post type in WordPress and I try to display posts by category but it's not working in the new custom post type.我在 WordPress 中创建了一个自定义帖子类型,我尝试按类别显示帖子,但它在新的自定义帖子类型中不起作用。 the Query that I wrote:我写的查询:

$args = ( array( 'post_type' => 'success','category_name' => 'biography') );
$succ = new WP_Query( $args ); ?>
 

And it shows nothing.它什么也没显示。

My custom taxonomy:我的自定义分类:

add_action( 'init', 'my_texonomy', 0 );

function my_texonomy() {


 $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('subjects',array('success'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'subject' ),
  ));
 
}

How should I display posts in this category?我应该如何显示这个类别的帖子?

You can use tax_query to get posts by a custom taxonomy.您可以使用tax_query按自定义分类法获取帖子。 try the below code.试试下面的代码。

$args = array( 
    'post_type' => 'success',
    'tax_query' => array(
        array(
            'taxonomy' => 'subjects', // custom taxonomy
            'field'    => 'slug',
            'terms'    => 'biography', // taxonomy term (category)
        )
    )
);
$succ = new WP_Query( $args ); 

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

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