2016-09-27 53 views
6

我创建了一个柱式与CPT UI的所有帖子和自定义文章类型:显示,通过使用一个单一的类别

add_action('init', 'cptui_register_my_cpts_matratze'); 
function cptui_register_my_cpts_matratze() { 
    $labels = array(
     "name" => __('Matratzen', ''), 
     "singular_name" => __('Matratze', ''), 
     ); 

    $args = array(
     "label" => __('Matratzen', ''), 
     "labels" => $labels, 
     "description" => "", 
     "public" => true, 
     "publicly_queryable" => true, 
     "show_ui" => true, 
     "show_in_rest" => false, 
     "rest_base" => "", 
     "has_archive" => true, 
     "show_in_menu" => true, 
       "exclude_from_search" => false, 
     "capability_type" => "post", 
     "map_meta_cap" => true, 
     "hierarchical" => false, 
     "rewrite" => array("slug" => "matratze", "with_front" => true), 
     "query_var" => true, 

     "supports" => array("title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "author", "page-attributes", "post-formats"),  
     "taxonomies" => array("category", "post_tag"), 
      ); 
    register_post_type("matratze", $args); 

// End of cptui_register_my_cpts_matratze() 
} 

然而,当我想在在我的前端的链接来访问类,我得到没有帖子。

例如,当您点击我得不到任何回报:

Category

的岗位上,有类别DaMi

Post

是我CPT UI Post Type配置错误?任何建议我做错了什么?

+0

不确定,但在$ args中添加'menu_position'=> 1。 https://codex.wordpress.org/Function_Reference/register_post_type –

+0

@DevDanidhariya我刚澄清了我的答案!这不是关于后端菜单,而是前端显示!无论如何Thx! – mrquad

+0

@mrquad:你还可以分享你的'register_taxonomy'代码。 –

回答

3

过目here

默认情况下,在你的WordPress网站类别页面将只显示默认的“帖子”岗位类型,所以你需要添加CPT WordPress的通过将pre_get_posts过滤查询上岗。

添加代码在这里:

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', 'matratze'); // don't forget nav_menu_item to allow menus to work! 
    $query->set('post_type',$post_type); 
    return $query; 
    } 
} 
+2

最好在你的回答中显示文章解释的内容,以防万一这个链接404s – Spartacus

2

当您使用WordPress的标准类别您的自定义后类型,默认情况下WordPress的将不显示该类别档案页上的自定义后类型职位。因此,您必须修改类别存档页面查询以包含您的自定义帖子类型。

请在function.php文件中包含以下代码。不要忘记在下面的代码中更改您的自定义帖子类型名称。

function add_custom_post_types_to_tax($query) { 
    if(is_category() || is_tag() && 
     empty($query->query_vars['suppress_filters'])) { 

     // Include your custom post type 
     $post_types = array('post', 'your_custom_type'); 

     $query->set('post_type', $post_types); 
     return $query; 
    } 
} 

add_filter('pre_get_posts', 'add_custom_post_types_to_tax'); 

希望这会帮助你。

1

为此,

function query_post_type($query) { 
    if(is_category()) { 
     $post_type = get_query_var('post_type'); 
     // post type get here 
     if($post_type){ 
       // no more code here for by default 
     }   
     else{ 
      $post_types = array('post', 'your_custom_type'); 
      // custom type   
     } 
     $query->set('post_type',$post_type); 
     return $query; 
    } 
} 
add_filter('pre_get_posts', 'query_post_type'); 

我想利用这个功能,它会工作。任何其他问题的评论。

相关问题