【问题标题】:Wordpress taxonomy & term templates (404)Wordpress 分类和术语模板 (404)
【发布时间】:2016-11-29 02:42:29
【问题描述】:

我想在我的网站上添加一个带有照片的部分。照片应在分类法和 custom_post_types 的帮助下进行分组。链接应该是这样的

  • mysite.com/photography => 分类模板
  • mysite.com/photography/underwater => 分类术语模板

我在 functions.php 中创建了分类和 custom_post_type,这是代码。

function photos_custom_post_type() {
$labels = array(
    'name'                => __( 'Photos' ),
    'singular_name'       => __( 'Photo'),
    'menu_name'           => __( 'Photos'),
    'parent_item_colon'   => __( 'Parent Photo'),
    'all_items'           => __( 'All Photos'),
    'view_item'           => __( 'View Photo'),
    'add_new_item'        => __( 'Add New Photo'),
    'add_new'             => __( 'Add New'),
    'edit_item'           => __( 'Edit Photo'),
    'update_item'         => __( 'Update Photo'),
    'search_items'        => __( 'Search Photo'),
    'not_found'           => __( 'Not Found'),
    'not_found_in_trash'  => __( 'Not found in Trash')
);
$args = array(
    'label'               => __( 'photos'),
    'description'         => __( 'Best Photos'),
    'labels'              => $labels,
    'supports'            => array( 'title','thumbnail',),
    'public'              => true,
    'hierarchical'        => false,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'has_archive'         => true,
    'can_export'          => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
    'taxonomies' => array('post_tag')
);
    register_post_type( 'photos', $args );
}
add_action( 'init', 'photos_custom_post_type', 0 );

// creating Taxonomy for Custom Post Type
add_action( 'init', 'photos_custom_taxonomy', 0 );
function photos_custom_taxonomy() {

$labels = array(
'name' => _x( 'Photos Category', 'taxonomy general name' ),
'singular_name' => _x( 'Photos Category', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Photos Category' ),
'all_items' => __( 'All Photos Category' ),
'parent_item' => __( 'Parent Photos Category' ),
'parent_item_colon' => __( 'Parent Photos Category:' ),
'edit_item' => __( 'Edit Photos Category' ), 
'update_item' => __( 'Update Photos Category' ),
'add_new_item' => __( 'Add New Photos Category' ),
'new_item_name' => __( 'New Photos Category Name' ),
'menu_name' => __( 'Photos Category' ),
);  

register_taxonomy('photos_cat',array('photos'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array('slug' => 'photography', 'with_front' => false)
));
}

我也创造了

  • taxonomy-post_cat-underwater.php => 这行得通
  • taxonomy-post_cat.php => 这不是

当我转到mysite.com/photography/underwater => 一切正常,但是当我转到mysite.com/photography => 我得到 404 错误..

请帮助我了解发生了什么问题。

【问题讨论】:

  • 在 wp-config.php 中设置 WP_DEBUG = true 。错误信息是什么?
  • @elicohenator 没有错误,只有 404 页

标签: php wordpress custom-post-type taxonomy


【解决方案1】:

您必须遵循 WP 开发人员文档中所述的分类模板规则:https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/#custom-taxonomy

自定义分类的层次结构如下所示:

  1. taxonomy-{taxonomy}-{term}.php:例如,如果分类命名为“sometax”,并且分类的术语是“someterm”,WordPress 将查找名为 taxonomy-sometax-someterm.php 的文件.
  2. taxonomy-{taxonomy}.php:例如,如果分类名为“sometax”,WordPress 将查找名为 taxonomy-sometax.php 的文件
  3. taxonomy.php
  4. archive.php
  5. index.php

【讨论】:

  • 我知道。我已经创建了 taxonomy-post_cat-underwater.php 和 ''taxonomy-post_cat.php 。第一个有效,第二个无效。
  • 那是因为更高层次的文件覆盖了第二个文件。更改文件名,您将看到它的工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多