2017-05-24 107 views
0

我在WordPress中创建了一个名为“找到”的自定义帖子类型。其中,我创建了两个分类法,“宠物”(称为petsfound)和“电子”(称为electronicsfound),这两个分类法都有不同的术语。WordPress自定义分类显示404

如果我查看使用分类术语网站上的帖子,他们表现出正确的,但是,试图查看职位的分类列表显示404。因此,发生以下情况:

https://example.com/found/pets-found/显示404页面。

https://example.com/found/pets-found/dog显示在宠物狗的列表中找到分类。

我已经使用archive.php和taxonomy-petsfound.php进行了测试,但都显示了分类的404。电子分类学也是如此。

以下是“发现” CPT代码和“petsfound”分类:

function found_custom_post_type() { 
    $labels = array(
     'name'     => _x('Found Items', 'Post Type General Name', 'lost_and_found'), 
     'singular_name'   => _x('Found', 'Post Type Singular Name', 'lost_and_found'), 
     'menu_name'    => __('Found', 'lost_and_found'), 
     'name_admin_bar'  => __('Found', 'lost_and_found'), 
     'archives'    => __('Item Archives', 'lost_and_found'), 
     'parent_item_colon'  => __('Parent Item:', 'lost_and_found'), 
     'all_items'    => __('All Items', 'lost_and_found'), 
     'add_new_item'   => __('Add New Item', 'lost_and_found'), 
     'add_new'    => __('Add New', 'lost_and_found'), 
     'new_item'    => __('New Item', 'lost_and_found'), 
     'edit_item'    => __('Edit Item', 'lost_and_found'), 
     'update_item'   => __('Update Item', 'lost_and_found'), 
     'view_item'    => __('View Item', 'lost_and_found'), 
     'search_items'   => __('Search Item', 'lost_and_found'), 
     'not_found'    => __('Not found', 'lost_and_found'), 
     'not_found_in_trash' => __('Not found in Trash', 'lost_and_found'), 
     'featured_image'  => __('Featured Image', 'lost_and_found'), 
     'set_featured_image' => __('Set featured image', 'lost_and_found'), 
     'remove_featured_image' => __('Remove featured image', 'lost_and_found'), 
     'use_featured_image' => __('Use as featured image', 'lost_and_found'), 
     'insert_into_item'  => __('Insert into item', 'lost_and_found'), 
     'uploaded_to_this_item' => __('Uploaded to this item', 'lost_and_found'), 
     'items_list'   => __('Items list', 'lost_and_found'), 
     'items_list_navigation' => __('Items list navigation', 'lost_and_found'), 
     'filter_items_list'  => __('Filter items list', 'lost_and_found'), 
    ); 
    $args = array(
     'label'     => __('Found', 'lost_and_found'), 
     'description'   => __('Found Post Type', 'lost_and_found'), 
     'labels'    => $labels, 
     'supports'    => array('title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields',), 
     'taxonomies'   => array('petsfound', 'electronicsfound', 'countyfound'), 
     'hierarchical'   => false, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'   => true, 
     'menu_position'   => 5, 
     'show_in_admin_bar'  => true, 
     'show_in_nav_menus'  => true, 
     'rewrite'    => true, 
     'can_export'   => true, 
     'has_archive'   => true,   
     'exclude_from_search' => false, 
     'publicly_queryable' => true, 
     'capability_type'  => 'page', 
    ); 
    register_post_type('found', $args); 
} 
add_action('init', 'found_custom_post_type', 5); 
} 
function found_taxonomies_pets() { 
    $labels = array(
     'name'    => _x('Pets', 'taxonomy general name'), 
     'singular_name'  => _x('Pet', 'taxonomy singular name'), 
     'search_items'  => __('Search Pets'), 
     'all_items'   => __('All Pets'), 
     'parent_item'  => __('Parent Pets'), 
     'parent_item_colon' => __('Parent Pet:'), 
     'edit_item'   => __('Edit Pet'), 
     'update_item'  => __('Update Pet'), 
     'add_new_item'  => __('Add New Pet'), 
     'new_item_name'  => __('New Pet'), 
     'menu_name'   => __('Pets'), 
    ); 
    $args = array(
     'labels' => $labels, 
     'hierarchical' => true, 
     'rewrite'  => array('slug' => 'found/pets-found', 'with_front' => true), 
     'show_admin_column' => true, 
     //'has_archive' => true 

    ); 
    register_taxonomy('petsfound', 'found', $args); 
    flush_rewrite_rules(); 
} 
add_action('init', 'found_taxonomies_pets', 1); 
  • 我已经重新在多个场合固定链接。
  • 我测试过将hierarchicalwith_front的值从真到假修改过来,以防万一它们有任何影响。
  • 我对404.php模板跑print_r($wp_query);和接收的query_vars开始执行以下操作:

    [query_vars] => Array ([page] => 0 [found] => pets-found [post_type] => found [name] => pets-found

不知道要寻找什么别的事情,所以希望有人能帮助。

干杯

达明

编辑 - 添加分类学,petsfound.php代码

<?php 
/** 
* The template for displaying Pets Found Taxonomy. 
* 
* Learn more: http://codex.wordpress.org/Template_Hierarchy 
* 
* @package dazzling 
*/ 

get_header(); ?> 



<div class="breadcrumb" typeof="BreadcrumbList" vocab="http://schema.org/"> 
    <?php if(function_exists('bcn_display')) 
    { 
     bcn_display(); 
    }?> 
</div> 
<?php print_r($wp_query); ?> 
     <section id="primary" class="content-area col-sm-12 col-md-12 <?php echo of_get_option('site_layout'); ?>"> 
      <main id="main" class="site-main" role="main"> 

      <?php if (have_posts()) : ?> 

       <header class="page-header"> 
        <h1 class="page-title"> 
         Found Items 2 
        </h1> 
        <?php 
         // Show an optional term description. 
         $term_description = term_description(); 
         if (! empty($term_description)) : 
          printf('<div class="taxonomy-description">%s</div>', $term_description); 
         endif; 
        ?> 
       </header><!-- .page-header --> 

       <?php /* Start the Loop */ ?> 
       <?php while (have_posts()) : the_post(); ?> 

        <?php 
         /* Include the Post-Format-specific template for the content. 
         * If you want to override this in a child theme, then include a file 
         * called content-___.php (where ___ is the Post Format name) and that will be used instead. 
         */ 
         get_template_part('content', get_post_format()); 
        ?> 

       <?php endwhile; ?> 

       <?php dazzling_paging_nav(); ?> 

      <?php else : ?> 

       <?php get_template_part('content', 'none'); ?> 

      <?php endif; ?> 

      </main><!-- #main --> 
     </section><!-- #primary --> 


<?php get_footer(); ?> 
+0

不知道这是否有任何影响,但要注册的分类法后类型职位类型已经注册了。 – inarilo

+0

如何?这是放在这个部分的首要吗? 'add_action('init','found_taxonomies_pets',1);'? – damienoneill2001

+0

是的。如果不同的功能以相同的优先级注册,则按照它们添加的顺序执行。 – inarilo

回答

0

据我知道,如果你加入,你可以访问自定义分类产品列表或任何您想要的参数传递参数的帮助,如上所示,如果您访问列表,您必须使用参数,如下面的url:

https://example.com/found/pets-found/dog这会给你的结果,因为这里的狗是参数,你有狗列表。

$custom_terms = get_terms('custom_taxonomy'); 
foreach($custom_terms as $custom_term) { 
wp_reset_query(); 
$args = array('post_type' => 'custom_post_type', 
    'tax_query' => array(
     array(
      'taxonomy' => 'custom_taxonomy', 
      'field' => 'slug', 
      'terms' => $custom_term->slug, 
     ), 
    ), 
); 

$loop = new WP_Query($args); 
if($loop->have_posts()) { 
    echo '<h2>'.$custom_term->name.'</h2>'; 

    while($loop->have_posts()) : $loop->the_post(); 
     echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; 
    endwhile; 
} 
} 

希望这将帮助你

+0

嗨,谢谢你的回答,但这没有帮助。如上所述,我访问/找到/宠物找到/狗没有问题。我的问题显示找到的/宠物找到的分类标准作为一个整体(所以如果我想看到该分类中所有术语的所有职位)。虽然谢谢! – damienoneill2001

+0

好吧,为什么你不使用为worpress创建一个新的页面,并使用这个函数$ custom_terms = get_terms('custom_taxonomy');这将帮助您为查询创建参数,然后您可以获取与分类相关的所有帖子。我已经更新了我的答案 –

+0

所以,为了澄清一下,您的解决方案是为网站上的所有自定义分类标准创建单独的页面和页面模板,还是只为它们提供一个模板?虽然我在示例中使用了2个分类法,但在完成的网站上会有大量的分类法,因此使用标准模板层次结构似乎是最合适的。 – damienoneill2001

0

当涉及到对文章类型和分类,有时你喜欢的一个先于其他规则匹配重写规则。也许从一个帖子或页面slu。。即使它是垃圾中的一个。

尝试检查所有重写规则并查看与您的URL请求匹配的内容。您可能需要更改创建帖子类型或分类法的顺序。尝试在您的帖子类型之前注册您的分类法。

你可以看到匹配重写规则在这个片段:

function debug_rewrite_rules() { 
    global $wp, $template, $wp_rewrite; 

    echo '<pre>'; 
    echo 'Request: '; 
    echo empty($wp->request) ? "None" : esc_html($wp->request) . PHP_EOL; 
    echo 'Matched Rewrite Rule: '; 
    echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule) . PHP_EOL; 
    echo 'Matched Rewrite Query: '; 
    echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query) . PHP_EOL; 
    echo 'Loaded Template: '; 
    echo basename($template); 
    echo '</pre>' . PHP_EOL; 

    echo '<pre>'; 
    print_r($wp_rewrite->rules); 
    echo '</pre>'; 
} 

add_action('wp_head', 'debug_rewrite_rules'); 
+0

嗨@stillatmylinux,感谢您的帮助,并将我推向自定义重写,但同时我现在正在得到一个结果页面,它似乎显示了所有的项目。 – damienoneill2001

相关问题