2016-11-28 167 views
0

我想在我的網站上添加一個包含照片的部分。照片應該在分類法和custom_post_types的幫助下分組。鏈接應該像 WordPress的分類和術語模板(404)

我在functions.php創建分類和custom_post_type這

  • mysite.com/photography =>分類模板
  • mysite.com/photography/underwater =>分類項模板,這裏是代碼。

    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錯誤..

    請幫我理解發生了什麼問題。

+0

在wp-config.php中設置WP_DEBUG = true。什麼是錯誤信息? – elicohenator

+0

@elicohenator沒有錯誤,只有404頁 – CroaToa

回答

0

,你必須遵循的分類模板規則的WP開發者文檔指出:https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/#custom-taxonomy

自定義分類的層次結構如下:

  1. taxonomy- {}分類 - {term} .php:例如,如果分類法被命名爲「sometax」,而分類法的術語是「someterm」,則WordPress將查找名爲taxonomy-sometax-someterm.php的文件。
  2. taxonomy- {}分類.PHP:例如,如果分類被命名爲「sometax,」 WordPress的會尋找分類,sometax.php
  3. 命名文件
  4. taxonomy.php
  5. archive.php index.php
+0

我知道。我已經創建了'taxonomy-post_cat-underwater.php'和''taxonomy-post_cat.php。首先是工作,其次不是。 – CroaToa

+0

這是因爲高級別文件覆蓋第二個文件。更改文件名稱,你會看到它的工作。 – elicohenator

相關問題