Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从自定义帖子类型类别中列出帖子?_Php_Wordpress_Custom Post Type - Fatal编程技术网

Php 从自定义帖子类型类别中列出帖子?

Php 从自定义帖子类型类别中列出帖子?,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我有一个自定义的帖子类型和一个像这样创建的公文包分类法: $args = array( 'label' => __('Portfolio', 'my-portfolio'), 'labels' => array( 'add_new_item' => __('New portfolio', 'my-portfolio'), 'new_item' => __('New portfolio'

我有一个自定义的帖子类型和一个像这样创建的公文包分类法:

    $args = array(
        'label' => __('Portfolio', 'my-portfolio'),
        'labels' => array(
            'add_new_item' => __('New portfolio', 'my-portfolio'),
            'new_item' => __('New portfolio', 'my-portfolio'),
            'not_found' => __('No portfolio items', 'my-portfolio'),
        ),
        'singular_label' => __('Portfolio', 'my-portfolio'),
        'menu_icon' => 'dashicons-portfolio',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'register_meta_box_cb' => 'add_remove_metaboxes_portfolio',
       );

    //Register type and custom taxonomy for type.
    register_post_type( 'portfolio' , $args );
    register_taxonomy("portfolio-category", array("portfolio"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true, "slug" => 'portfolio-category',"show_in_nav_menus"=>false));
当我在查询中使用它时,它的工作原理应该是这样的。它显示在后端的菜单中,以及此自定义帖子类型的类别

我的问题是,当我尝试从某个类别检索所有帖子时(通过单击类别名称),我发现
没有找到任何帖子。对不起在我的页面上

该页面是一个默认的wordpress存档,应该只列出所有帖子,但没有显示任何内容。url如下所示:

    $args = array(
        'label' => __('Portfolio', 'my-portfolio'),
        'labels' => array(
            'add_new_item' => __('New portfolio', 'my-portfolio'),
            'new_item' => __('New portfolio', 'my-portfolio'),
            'not_found' => __('No portfolio items', 'my-portfolio'),
        ),
        'singular_label' => __('Portfolio', 'my-portfolio'),
        'menu_icon' => 'dashicons-portfolio',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'register_meta_box_cb' => 'add_remove_metaboxes_portfolio',
       );

    //Register type and custom taxonomy for type.
    register_post_type( 'portfolio' , $args );
    register_taxonomy("portfolio-category", array("portfolio"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true, "slug" => 'portfolio-category',"show_in_nav_menus"=>false));
http://xampp/my-theme/?portfolio-类别=动物

但即使我使用post name permalinks,也没有任何效果

我在
functions.php

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'portfolio', 'nav_menu_item'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

但这也没用。我在这里遗漏了什么?

您必须为自定义帖子类型使用归档页面模板。请把代码放在自定义的帖子类型中

$args = array(
        'label' => __('Portfolio', 'my-portfolio'),
        'labels' => array(
            'add_new_item' => __('New portfolio', 'my-portfolio'),
            'new_item' => __('New portfolio', 'my-portfolio'),
            'not_found' => __('No portfolio items', 'my-portfolio'),
        ),
        'singular_label' => __('Portfolio', 'my-portfolio'),
        'menu_icon' => 'dashicons-portfolio',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'register_meta_box_cb' => 'add_remove_metaboxes_portfolio',
       );

    //Register type and custom taxonomy for type.
    register_post_type( 'portfolio' , $args );
flush_rewrite_rules();

不要以您所使用的方式使用
flush\u rewrite\u rules()
。它运行起来非常昂贵,而且资源非常紧张。如果没有刷新规则,它会减慢页面加载速度。怎么可能调用页面模板?请执行一次并删除它。法典列出了应该使用的钩子。你做的和我写的有什么不同?除了
flush_rewrite_rules()调用自定义帖子类型模板无需执行任何操作。如果您不打算使用父子关系,请在cpt中将Hierarchy设置为false。我可能会使用父子关系,类似于显示所有父类别,然后当您进入其中一个类别时,您可以在其中看到子类别和帖子。