Wordpress分类法&术语模板(404)

时间:2016-11-28 17:36:18

标签: php wordpress custom-post-type taxonomy

我想在我的网站上添加一个带照片的部分。应使用分类法和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错误..

请帮助我理解出了什么问题。

1 个答案:

答案 0 :(得分:0)

您必须遵循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