2017-04-16 180 views
0

我在WordPress 4.7.3中創建了一個包含分類法的自定義帖子類型,但由於某種原因,分類法頁面無法正常工作。我的自定義帖子類型是「events」,但是當訪問「mysite.com/events/event-category」或訪問「mysite.com/events」時,archive-events.php模板文件不起作用自定義帖子類型的問題

下面是我從functions.php的代碼,我在這裏做錯了什麼?笑

   add_action('init', 'register_events', 20); 
      function register_events() { 
       $labels = array(
        'name' => _x('All Events', 'events','sonal'), 
        'singular_name' => _x('Event', 'events', 'sonal'), 
        'add_new' => _x('Add New', 'events', 'sonal'), 
        'add_new_item' => _x('Add New Event', 'events', 'sonal'), 
        'edit_item' => _x('Edit Event', 'events', 'sonal'), 
        'new_item' => _x('New Event', 'events', 'sonal'), 
        'view_item' => _x('View Event', 'events', 'sonal'), 
        'search_items' => _x('Search Events', 'events', 'sonal'), 
        'not_found' => _x('No Events found...', 'events', 'sonal'), 
        'not_found_in_trash' => _x('No Events found in Trash', 'events', 'sonal'), 
        'parent_item_colon' => _x('Parent Event:', 'events', 'sonal'), 
        'menu_name' => _x('Events', 'events', 'sonal'), 
       ); 

       $args = array(
        'labels' => __($labels, 'local'), 
        'hierarchical' => true, 
        'description' => 'events', 
        'supports' => array('title', 'editor', 'excerpt', 'author', 'revisions'), 
        'taxonomies' => array('events_category'), 
        'show_ui' => true, 
        'show_in_menu' => true, 
        'menu_position' => 5, 
        'menu_icon' => 'dashicons-tickets-alt', 
        'show_in_nav_menus' => true, 
        'publicly_queryable' => true, 
        'exclude_from_search' => false, 
        'query_var' => true, 
        'can_export' => true, 
        'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE), 
        'public' => true, 
        'has_archive' => 'events_category', 
        'capability_type' => 'post' 
       ); 
       register_post_type('events', $args); 
      } 
      //Create Taxonomies (Categories) 
      add_action('init', 'create_events_taxonomies', 20); 
      function create_events_taxonomies() { 
       $labels = array(
        'name'    => _x('Event Categories', 'taxonomy general name', 'sonal'), 
        'singular_name'  => _x('Event Category', 'taxonomy singular name', 'sonal'), 
        'search_items'  => __('Search Event Categories', 'sonal'), 
        'all_items'   => __('All Event Categories', 'sonal'), 
        'parent_item'  => __('Parent Event Category', 'sonal'), 
        'parent_item_colon' => __('Parent Event Category:', 'sonal'), 
        'edit_item'   => __('Edit Event Category', 'sonal'), 
        'update_item'  => __('Update Event Category', 'sonal'), 
        'add_new_item'  => __('Add New Event Category', 'sonal'), 
        'new_item_name'  => __('New Event Category Name', 'sonal'), 
        'menu_name'   => __('Event Categories', 'sonal'), 
       ); 

       $args = array(
        'hierarchical'  => true, 
        'labels'   => $labels, 
        'show_ui'   => true, 
        'show_admin_column' => true, 
        'query_var'   => true, 
        'rewrite'   => array('slug' => 'events', 'with_front' => false), 
       ); 

       register_taxonomy('events_category', array('events'), $args); 
      } 
      //Set Permalinks 
      function wpa_events_permalinks($post_link, $post){ 
       if (is_object($post) && $post->post_type == 'events'){ 
        $terms = wp_get_object_terms($post->ID, 'events_category'); 
        if($terms){ 
         return str_replace('%events_category%' , $terms[0]->slug , $post_link); 
        } 
       } 
       return $post_link; 
      } 
      add_filter('post_type_link', 'wpa_events_permalinks', 1, 2); 

回答

2

刪除/%events_category%來自於事件的重寫參數自定義後類型

前:

'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE), 

後:

'rewrite' => array('slug' => 'events','with_front' => FALSE), 

再在ARGS的事件自定義帖子類型,將「has_archive」設置爲true

之前

'has_archive' => 'events_category', 

後:

'has_archive' => true, 

在設置刷新永久鏈接 - >永久鏈接 要確保你選擇的自定義結構

/%category%/%postname%/ 

然後去

yoursite.com/events/ 

archive-events.php文件應該可以工作

相關問題