如何解决请检查我的代码如何为新的自定义帖子类型创建单独的类别
function create_post_type_opportunities() {
register_post_type( 'opportunities',// CPT Options
array(
'labels' => array(
'name' => __( 'Opportunities'),'singular_name' => __( 'Opportunities')
),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'opportunities'),'supports' => array('title','thumbnail','editor','icon' ),)); }
// Hooking up our function to theme setup
add_action( 'init','create_post_type_opportunities');
/* Custom Post Type for our Add opportunities*/
这是我的代码,请检查我的代码如何为新的自定义帖子类型创建单独的类别
解决方法
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies','Post Type General Name','twentythirteen' ),'singular_name' => _x( 'Movie','Post Type Singular Name','menu_name' => __( 'Movies','parent_item_colon' => __( 'Parent Movie','all_items' => __( 'All Movies','view_item' => __( 'View Movie','add_new_item' => __( 'Add New Movie','add_new' => __( 'Add New',);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies','description' => __( 'Movie news and reviews','labels' => $labels,'supports' => array( 'title','editor','excerpt','author','thumbnail','comments','revisions','custom-fields',),'hierarchical' => false,'public' => true,'show_ui' => true,'show_in_menu' => true,'show_in_nav_menus' => true,'show_in_admin_bar' => true,'menu_position' => 5,'can_export' => true,'has_archive' => true,'exclude_from_search' => false,'publicly_queryable' => true,'capability_type' => 'page','show_in_rest' => true,// This is where we add taxonomies to our CPT
'taxonomies' => array( 'category' ),);
// Registering your Custom Post Type
register_post_type( 'movies',$args );
}
add_action( 'init','custom_post_type',0 );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init',0 );
To display your custom post types on the same category page as your default posts,you need to add this code into your theme’s functions.php or a site-specific plugin.
add_filter('pre_get_posts','query_post_type');
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item','post','movies'); // don't forget nav_menu_item `enter code here`to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。