【问题标题】:Wordpress - custom taxonomy - no archiveWordpress - 自定义分类法 - 无存档
【发布时间】:2016-08-18 23:30:14
【问题描述】:

基本上,我的网站是基于一个缺少大量代码的主题构建的,而这些代码应该存在。我已经设法解决了除此之外的所有问题。

该网站具有投资组合帖子类型。基本上正在创建类别,并且可以将帖子分配给这些类别。帖子已创建,但类别的存档页面返回“找不到页面”。

我的functions.php部分如下:

  /**  taxonomy.
--------------------------------------------------------------------------------------------------- */

function create_portfolio_taxonomy()
{
    register_taxonomy(
        'portfolio-category',
        'portfolio',
        array(
            'labels'            => array(
                'name'              => _x('Categories', 'portfolio', 'awe'),
                'singular_name'     => _x('Category', 'portfolio', 'awe'),
                'menu_name'         => __('Categories', 'awe'),
                'all_items'         => __('All Categories', 'awe'),
                'edit_item'         => __('Edit Category', 'awe'),
                'view_item'         => __('View Category', 'awe'),
                'update_item'       => __('Update Category', 'awe'),
                'add_new_item'      => __('Add New Category', 'awe'),
                'new_item_name'     => __('New Category Name', 'awe'),
                'parent_item'       => __('Parent Category', 'awe'),
                'parent_item_colon' => __('Parent Category:', 'awe'),
                'search_items'      => __('Search Categories', 'awe'),
            ),
            'show_admin_column' => true,
            'hierarchical'      => true,
            'rewrite'           => array(
                'slug' => 'category',
            ),
        )
    );
}

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

add_action('init', 'custom_taxonomy_flush_rewrite');
function custom_taxonomy_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}


/*------------------------------------------------------------------------------------------------------------------*/
/*  custom post type 
/*------------------------------------------------------------------------------------------------------------------*/ 

add_action( 'init', 'create_portfolio_post_type' );
function create_portfolio_post_type() {
  register_post_type( 'portfolio',
    array(
      'labels' => array(
        'name' => __( 'Portfolio' ),
        'singular_name' => __( 'Portfolio' )
      ),
      'menu_icon' => 'dashicons-portfolio',
      'hierarchical' => 'true',
      'public' => true,
      'has_archive' => true,
    )
  );
}

如您所见,我可以通过主页上的投资组合部分的类别进行排序 - willd.co.uk/#clients

帖子页面 - http://willd.co.uk/portfolio/victoria-front-of-house/ 无法使用的类别页面 - http://willd.co.uk/category/eggs/

从我收集到的 Wordpress 经历了潜在模板的层次结构,然后将来到 archive.php

我是 php 新手,所以真的只是使用教程和逻辑来让一切正常工作。

我已经刷新了永久链接。

我在想也许我需要创建和编辑一个portfolio-archive.php 或taxonomy.php 文件,而不是让它落入archive.php,尽管我看过教程和其他stackoverflow 答案我还没有解决这个问题。

非常感谢您的帮助,

W

【问题讨论】:

  • WordPress 默认博客类别具有“类别”的名称,因此最好将其命名为其他名称,因为它可能会试图覆盖它。还可以尝试创建 taxonomy-category.php 文件并在主题根目录中使用存档页面的内容填充它
  • 谢谢你,理查德 - 我认为蛞蝓正在覆盖它!

标签: php wordpress wordpress-theming taxonomy custom-taxonomy


【解决方案1】:

最好使用数组来安排它,您会注意到很多事情,将 slug 更改为投资组合类别,我认为它现在可以工作了。

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

    function create_portfolio_taxonomies(){
            $labels = array(
                    'name'              => _x('Categories', 'portfolio', 'awe'),
                    'singular_name'     => _x('Category', 'portfolio', 'awe'),
                    'menu_name'         => __('Categories', 'awe'),
                    'all_items'         => __('All Categories', 'awe'),
                    'edit_item'         => __('Edit Category', 'awe'),
                    'view_item'         => __('View Category', 'awe'),
                    'update_item'       => __('Update Category', 'awe'),
                    'add_new_item'      => __('Add New Category', 'awe'),
                    'new_item_name'     => __('New Category Name', 'awe'),
                    'parent_item'       => __('Parent Category', 'awe'),
                    'parent_item_colon' => __('Parent Category:', 'awe'),
                    'search_items'      => __('Search Categories', 'awe'),
                );

               $args = array('hierarchical'      => true,
                             'labels'            => $labels,
                             'show_ui'           => true,
                             'show_admin_column' => true,
                             'query_var'         => true,
                             'rewrite'           => array(
                                                       'slug' => 'portfolio-category',
                                                    );
                         );
        register_taxonomy( 'portfolio-category', array( 'portfolio' ), $args );

    }

您可以查看的另一个资源是 zilla-portfolio 另一个是 sequence slider 这些插件已经很好地使用了它。

【讨论】:

  • 我觉得这里有错别字---第四行代码应该完全删除? 数组(>>
  • @user3445853 你说得对我的朋友,谢谢你的指出,我现在已经更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2012-06-21
  • 2014-07-20
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多