我有一个名为“news”的自定义帖子类型,还有一个名为“news category”的自定义分类法。我想创建一个wp查询,以返回特定“新闻类别”(例如“press”)的所有“新闻”帖子。有没有一种通用的方法来写这篇文章,这样我就不需要为每个“新闻类别”写一篇了?
我认为这与
$args = array(
\'post_type\' => \'news\',
\'news category\' => *GET THE LABEL FROM THE PAGE SLUG SOMEHOW*
);
$query = new WP_Query( $args );
编辑:这是我的函数代码。php文件
function my_custom_post_news() {
$labels = array(
\'name\' => _x( \'News\', \'post type general name\' ),
\'singular_name\' => _x( \'News\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => __( \'Add New News\' ),
\'edit_item\' => __( \'Edit News\' ),
\'new_item\' => __( \'New News\' ),
\'all_items\' => __( \'All News\' ),
\'view_item\' => __( \'View News\' ),
\'search_items\' => __( \'Search News\' ),
\'not_found\' => __( \'No News found\' ),
\'not_found_in_trash\' => __( \'No News found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'News\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds our news and news specific data\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
\'has_archive\' => true,
);
register_post_type( \'news\', $args );
}
add_action( \'init\', \'my_custom_post_news\' );
function mav_taxonomies_news() {
$labels = array(
\'name\' => _x( \'News Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'News Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search News Categories\' ),
\'all_items\' => __( \'All News Categories\' ),
\'parent_item\' => __( \'Parent News Category\' ),
\'parent_item_colon\' => __( \'Parent News Category:\' ),
\'edit_item\' => __( \'Edit News Category\' ),
\'update_item\' => __( \'Update News Category\' ),
\'add_new_item\' => __( \'Add New News Category\' ),
\'new_item_name\' => __( \'New News Category\' ),
\'menu_name\' => __( \'News Categories\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'has_archive\' => true,
);
register_taxonomy( \'news_category\', \'news\', $args );
register_taxonomy_for_object_type( \'news_category\', \'news\' );
}
add_action( \'init\', \'mav_taxonomies_news\', 0 );
/news
工程和
/news/sample-news-article
作品然而
news_category/events
给了我所有
news
项目,而不仅仅是
event
项目。
编辑:将“标签”更改为“新闻类别”