2017-08-02 171 views
0

當我切換頁面時,我正在創建自己的帖子類型(自定義帖子類型),並且出現錯誤。例如,當我打開一個類別時,它顯示一個帖子,但是當我轉到其他頁面時,例如,第二個,它顯示一個404(未找到)錯誤。下一個分類頁面404 wordpress自定義帖子類型

頁沒有錯誤 - WWW *****,錯誤/分類/ CAT-名稱/
頁 - WWW ***** /分類/ CAT-名稱/頁/ 2

這是我的分類法portfolio_categories.php文件

<div class="container page min-height"> 
    <div class="row works infinite-container"> 
     <div class="col-md-12 category-menu"> 
     <?php 
      $taxonomy = 'portfolio_categories'; 
      $cat = get_the_terms($post->ID, $taxonomy); 
      foreach ($cat as $cat_slug){$category_slug = $cat_slug->slug;} 
      $terms = get_terms($taxonomy); 
       if ($terms && !is_wp_error($terms)) : 
      ?> 

      <script> 
       $(document).ready(function(){ 
        var categoryClass = '<?php echo $category_slug ?>'; 
        $('.'+categoryClass).addClass('current'); 
       }); 
      </script> 

       <ul> 
        <?php foreach ($terms as $term) { ?> 
         <li class="<?php echo $term->slug; ?>"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li> 
        <?php } ?> 
       </ul> 
      <?php endif;?> 
     </div> 

     <?php   
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
      $args = array(
       'post_type' => 'portfolio', 
       'taxonomy' => $taxonomy, 
       'posts_per_page' => 2, 
       'portfolio_categories' => $category_slug, 
       'paged' => $paged 
      ); 
      $temp = $wp_query; $wp_query= null; 

      $wp_query = new WP_Query($args); 
      while ($wp_query->have_posts()) : $wp_query->the_post(); 
      $category = get_the_terms($post->ID, 'portfolio_categories'); 
     ?> 
      <div class="col-md-4 col-sm-6 col-xs-12 work"> 
       <div class="placeholder"> 
        <a href="<?php the_permalink(); ?>" class="work-link"></a> 
        <div class="work-overlay"> 
         <div class="overlay-content"> 
          <div class="category-name"><?php foreach ($category as $cat){echo $cat->name;}?></div> 
          <div class="work-title"><?php the_title(); ?></div> 
         </div> 
        </div> 
        <div class="work-thumb"> 
         <div style="background-image:url('<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>')"></div> 
        </div> 
       </div> 
      </div> 
     <?php 
     endwhile; 
      if(have_posts() == NULL){ 
       echo info("Sorry, but there is no content available at this time!"); 
      } 
     ?> 

     <?php 
      if (function_exists('custom_pagination')) { 
       custom_pagination($wp_query->max_num_pages,"",$paged); 
      } 
     ?> 
    </div> 
</div> 

有我的分類學PHP和register_post_type

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

    $args = array(
     'hierarchical'  => true, // Set this to 'false' for non-hierarchical taxonomy (like tags) 
     'labels'   => $labels, 
     'show_ui'   => true, 
     'show_admin_column' => true, 
     'query_var'   => true, 
     'rewrite'   => array('slug' => 'cat', 'with_front' => false), 
    ); 

    register_taxonomy('portfolio_categories', array('portfolio'), $args); 
} 
add_action('init', 'create_portfolio_taxonomies', 0); 

function cptui_register_my_cpts_portfolio() { 

    /** 
    * Post Type: Portfolio. 
    */ 

    $labels = array(
     "name" => __("Portfolio", ""), 
     "singular_name" => __("portfolio", ""), 
     "menu_name" => __("Portfolio", ""), 
     "all_items" => __("All portfolios", ""), 
     "edit_item" => __("Edit", ""), 
     "view_item" => __("View", ""), 
     "view_items" => __("View portfolio", ""), 
     "not_found" => __("Portfolio not found", ""), 
    ); 

    $args = array(
     "label" => __("Portfolio", ""), 
     "labels" => $labels, 
     "description" => "portfolio page", 
     "public" => true, 
     "publicly_queryable" => true, 
     "show_ui" => true, 
     "show_in_rest" => false, 
     "rest_base" => "", 
     "has_archive" => 'portfolio', 
     "show_in_menu" => true, 
     "exclude_from_search" => false, 
     "capability_type" => "post", 
     "map_meta_cap" => true, 
     "hierarchical" => false, 
     'rewrite' => array('slug' => 'cat', 'with_front' => false), 
     "query_var" => true, 
     "supports" => array("title", "editor", "thumbnail", "excerpt", "custom-fields", "multi-post-image"), 
     "taxonomies" => array("portfolio_categories"), 
    ); 

    register_post_type("portfolio", $args); 
} 

add_action('init', 'cptui_register_my_cpts_portfolio'); 

分頁功能

function custom_pagination($numpages = '', $pagerange = '', $paged='') { 

    if (empty($pagerange)) { 
    $pagerange = 5; 
    } 

    global $paged; 
    if (empty($paged)) { 
    $paged = 1; 
    } 
    if ($numpages == '') { 
    global $wp_query; 
    $numpages = $wp_query->max_num_pages; 
    if(!$numpages) { 
     $numpages = 1; 
    } 
    } 

    $pagination_args = array(
    'base'   => get_pagenum_link(1) . '%_%', 
    'format'   => 'page/%#%', 
    'total'   => $numpages, 
    'current'   => $paged, 
    'show_all'  => False, 
    'end_size'  => 1, 
    'mid_size'  => $pagerange, 
    'prev_next'  => True, 
    'prev_text'  => __('&laquo;'), 
    'next_text'  => __('&raquo;'), 
    'type'   => 'plain', 
    'add_args'  => false, 
    'add_fragment' => '' 
); 

    $paginate_links = paginate_links($pagination_args); 

    if ($paginate_links) { 
    echo "<nav class='pagination'>"; 
     echo $paginate_links; 
    echo "</nav>"; 
    } 

} 
+0

你能把'with_front => false'設置爲'with_front => true'嗎? – justkidding96

+0

@ justkidding96同樣的錯誤 –

+0

'/ category/cat-name/page/2'這是什麼意思?這是否涉及第二頁? – justkidding96

回答

0

從重寫選項中刪除slug => 'category',它應該是工作。

+0

那不是問題 –

+0

即時更新我的​​帖子與taxonomy-portfolio_categories.php –

0

此代碼完美地爲我工作。如果仍有問題,請嘗試將固定鏈接設置更改爲「發佈名稱」。

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

    $args = array(
     'hierarchical'  => true, // Set this to 'false' for non-hierarchical taxonomy (like tags) 
     'labels'   => $labels, 
     'show_ui'   => true, 
     'show_admin_column' => true, 
     'query_var'   => true, 
     'rewrite'   => array('slug' => 'portfolio-category', 'with_front' => false), 
    ); 

    register_taxonomy('portfolio_categories', array('portfolio'), $args); 
    flush_rewrite_rules(); 
} 
add_action('init', 'create_portfolio_taxonomies', 0); 

function cptui_register_my_cpts_portfolio() { 

    /** 
    * Post Type: Portfolio. 
    */ 

    $labels = array(
     "name" => __("Portfolio", ""), 
     "singular_name" => __("portfolio", ""), 
     "menu_name" => __("Portfolio", ""), 
     "all_items" => __("All portfolios", ""), 
     "edit_item" => __("Edit", ""), 
     "view_item" => __("View", ""), 
     "view_items" => __("View portfolio", ""), 
     "not_found" => __("Portfolio not found", ""), 
    ); 

    $args = array(
     "label" => __("Portfolio", ""), 
     "labels" => $labels, 
     "description" => "portfolio page", 
     "public" => true, 
     "publicly_queryable" => true, 
     "show_ui" => true, 
     "show_in_rest" => false, 
     "rest_base" => "", 
     "has_archive" => 'portfolio', 
     "show_in_menu" => true, 
     "exclude_from_search" => false, 
     "capability_type" => "post", 
     "map_meta_cap" => true, 
     "hierarchical" => false, 
     'rewrite' => array('slug' => 'portfolio', 'with_front' => false), 
     "query_var" => true, 
     "supports" => array("title", "editor", "thumbnail", "excerpt", "custom-fields", "multi-post-image"), 
     "taxonomies" => array("portfolio_categories"), 
    ); 

    register_post_type("portfolio", $args); 
    flush_rewrite_rules(); 
} 

add_action('init', 'cptui_register_my_cpts_portfolio'); 
  • 改寫塞「類別」默認使用後的類別,這就是爲什麼可能是你所得到的404錯誤。
+0

我替換此代碼並設置永久鏈接設置爲「發佈名稱」,但仍然是相同的錯誤。也許我的分頁有錯誤?我把它添加到我的帖子 –

+0

即時更新我的​​帖子與taxonomy-portfolio_categories.php –

+0

我已經測試這個代碼在我的本地服務器,並完美工作。可能會有任何插件影響到這一點。嘗試將永久鏈接鏈接更改爲默認鏈接,然後再次更改爲漂亮鏈接。 –

相關問題