【问题标题】:Why am I getting a 404 error when trying to reach the taxonomy.php page without any terms, (Wordpress don't see taxonomy page)为什么在没有任何条款的情况下尝试访问 taxonomy.php 页面时出现 404 错误,(Wordpress 看不到分类页面)
【发布时间】:2020-09-21 07:20:46
【问题描述】:

我正在尝试创建分类页面,因此我使用分类taxonomy-nowe.php 创建了分类

但是我的 WP 没有看到该页面,或者我把重写 URL 弄乱了?有人可以检查我的代码,看看我做错了什么。我通过保存 Plain 刷新了永久链接,然后返回到 Post name permalinks。但我没有得到结果。

我想实现这样的想法:domain.pl/oferta/kopiarki/nowe

// custom post type
function td_devices_posttype() {
 $labels = array(
     'name'                => _x( 'Kopiarki', 'Ogólna nazwa wpisów', 'textdomain' ),
     'singular_name'       => _x( 'Kopiarka', 'Pojedyńcza nazwa wpisu', 'textdomain' ),
     'menu_name'           => esc_html__( 'Kopiarki', 'textdomain' ),
     'parent_item_colon'   => esc_html__( 'Rodzic', 'textdomain' ),
     'all_items'           => esc_html__( 'Wszystkie kopiarki', 'textdomain' ),
     'view_item'           => esc_html__( 'Wyświetl kopiarki', 'textdomain' ),
     'add_new_item'        => esc_html__( 'Dodaj nową kopiarkę', 'textdomain' ),
     'add_new'             => esc_html__( 'Dodaj nową', 'textdomain' ),
     'edit_item'           => esc_html__( 'Edytuj Kopiarkę', 'textdomain' ),
     'update_item'         => esc_html__( 'Zaktualizuj kopiarkę', 'textdomain' ),
     'search_items'        => esc_html__( 'Szukaj kopiarkę', 'textdomain' ),
     'not_found'           => esc_html__( 'Nie zanleziono', 'textdomain' ),
     'not_found_in_trash'  => esc_html__( 'Nie znaleziono w koszu', 'textdomain' )
 );
 $args = array(
     'label'               => esc_html__( 'kopiarki', 'textdomain' ),
     'description'         => esc_html__( 'Wszystkie kopiarki', 'textdomain' ),
     'labels'              => $labels,
     'taxonomies'          => array( 'nowe' ),
     'hierarchical'        => true,
     'public'              => true,
     'show_ui'             => true,
     'show_in_menu'        => true,
     'show_in_nav_menus'   => true,
     'show_in_admin_bar'   => true,
     'menu_position'       => 100,
     'can_export'          => true,
     'has_archive'         => true,
     'exclude_from_search' => false,
     'publicly_queryable'  => true,
     'query_var'           => true,
     'show_admin_column'   => true,
     'capability_type'     => 'page',
     'rewrite'             => array('slug' => 'oferta/kopiarki'),
     'supports'            => array( 'title','editor','thumbnail', 'custom-fields')
 );
 register_post_type( 'kopiarki', $args );
  //flush_rewrite_rules();
}
add_action( 'init', 'td_devices_posttype' );

// custom taxonomies new
function new_posttype_taxonomy() {
    $labels = array(
        'name'                  => 'Nowe',
        'singular_name'         => 'Nowa',
        'search_items'          => 'Szukaj nowych',
        'all_items'             => 'Wszystkie nowe kopiarki',
        'parent_item'           => 'Rodzic',
        'parent_item_colon'     => 'Dwukropek elementu nadrzędnego:',
        'edit_item'             => 'Edytuj nową kopiarkę',
        'update_item'           => 'Zaktualizuj nową kopiarkę',
        'add_new_item'          => 'Dodaj nową kopiarkę',
        'new_item_name'         => 'Nowa nazwa kopiarki',
        'menu_name'             => 'Nowe'
    );

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

【问题讨论】:

    标签: php wordpress url-rewriting custom-post-type taxonomy


    【解决方案1】:

    这篇文章已经被编辑了很多次,因为当时我没有关于这个问题的所有信息。从那以后,我对该主题进行了广泛的研究。下面直奔主题和答案。


    我们在这里有两个目标,将永久链接结构设置为以下
    domain.com/_custom_post_type_/_taxonomy_/_term_/ ...

    并获取 taxonomy.php 以返回与自定义帖子类型的特定分类部分关联的帖子和术语的索引,而不是 404.php 页面。


    "当访问者点击指向类别、标签或自定义的超链接时 分类法,WordPress 按时间倒序显示一页帖子 按该分类过滤的顺序。”

    来源:@https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/

    用于分类的默认模板是taxonomy.php。这些文件使您可以定位特定的分类法或特定的分类法术语。例如:taxonomy-{taxonomy}-{term}.phptaxonomy-{taxonomy}.php

    在您的情况下,当您搜索 ./fruits/apples 时,您将获得与术语 apples 相关的所有帖子,这些帖子要么显示在 taxonomy.php 上,要么显示在 taxonomy-fruits.php 上,或者最后显示在 taxonomy-fruits-aples.php 上。 但是如果你想访问./fruits/呢? 为什么访问水果会返回 404.php 错误? 因为taxonomy.php 旨在显示针对术语的帖子,而不是针对分类的帖子。 这是正常的预期行为

    这要追溯到 10 年前的 https://core.trac.wordpress.org/ticket/13816,并提出了多个问题。 (有趣的阅读,你应该看看)。

    当您搜索./fruits/apples/ 时,我们会得到一个"true",但是如果用户搜索./fruits/./fruits,我们会得到一个"false",如果用户搜索./fruits,他应该被重定向到一个包含所有相关 fruits 帖子或条款的模板,因为这不是默认行为,我想出了一个案例解决方案:

    <?php add_action( 'template_redirect', 'fallback_custom_taxonomy_fruits' );
    function fallback_custom_taxonomy_fruits() {
      $url = $_SERVER[ 'REQUEST_URI' ];
      if ( ! is_tax( 'fruits' ) && substr( $url, -7 ) == '/fruits' || substr( $url, -8 ) == '/fruits/' ) {
        include( get_template_directory() . '/fruits.php' );
        exit();
      };
    }; ?>
    

    关于substr,值应与您的分类标签等于 WITH dashes 的字符数匹配。 substr( $url, -7 ) == '/fruits'

    这可能不是最好的方法,但它可以完成工作并且速度非常快。我没有看到任何其他有效的解决方案(2020 年 9 月)。

    fruits.php 是您的后备模板,以防用户搜索 ./fruits/./fruits。我们将 url 的末尾与我们的自定义分类 slug 匹配。只有当 is_tax() 为 false ! is_tax( 'fruits' ) 并且 URL 与我们的分类相匹配时才会发生这种情况。

    现在假设我们的自定义分类与称为“食谱”的自定义帖子类型相关联。然后,我们可以使用以下内容在我们的后备 fruit.php 模板文件中显示自定义帖子类型的帖子和相关术语:

    <?php
    $custom_post_type = 'recipes';
    $taxonomy = 'fruits';
    
    $terms = get_terms( $taxonomy );
    foreach ( $terms as $term ) {
    wp_reset_query();
    $args = array(
      'post_type' => $custom_post_type,
      'tax_query' => array(
      array(
        'taxonomy' => $taxonomy,
        'field' => 'slug',
        'terms' => $term->slug,
      ),
    ), );
    
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
      echo $term->name.'<br/>';
      while ( $query->have_posts() ) : $query->the_post();
      echo '<a href="'.get_permalink().'">'.the_title().'</a><br>';
      endwhile;
    };
    
    }; ?>
    

    此外,您还希望在您的固定链接中,在您的分类之前为您的自定义分类提供自定义帖子类型 (CPT),以获得如下结构: recipes/fruits/apples 其中 recipes 是您的自定义帖子类型。您可以在使用以下内容注册该分类的参数时使用重写规则:'rewrite' =&gt; array( 'slug' =&gt; 'recipes/fruits' ), hierarchical =&gt; true, ),

    add_action( 'init', 'custom_taxonomy_nowe' );
    function custom_taxonomy_nowe() {
      $custom_post_type = 'Recipes';
      $singular = 'Fruit';
      $plural = 'Fruits';
      $labels = array(
        'name' => $plural,
        'singular_name' => $singular,
      );
      $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => strtolower( $custom_post_type.'/'.$plural ), 'hierarchical' => true, ),
      );
      register_taxonomy( strtolower( $plural ), strtolower( $custom_post_type ), $args );
    };
    

    我们的永久链接结构现在应该如下所示
    domain.com/recipes/fruits/apples/ ...
    domain.com/_custom_post_type_/_taxonomy_/_term_/ ...

    分类页面现在可以用作帖子和分类索引。

    【讨论】:

    • 评论不用于扩展讨论;这个对话是moved to chat
    • amarinediary 这是一个非常有趣的案例,因为对我来说,作为一个有一些 SEO 经验的人,升一级是很自然的,我很惊讶这不是那样工作的。我认为这在 WP 层次结构中是可能的。如果 taxonomy- $ taxonomy.php 页面对分类法的作用与 taxonomy- $ taxonomy- $ term.php 的条款相同,那就太好了。我希望有一天 WP 程序员会注意到有一个可以填补的空白
    • 我没有尝试重定向到 taxonomy-$taxonomy.php 模板,但我猜它会起作用,因为唯一阻止它起作用的事实是,如果没有条款,我们会得到“假”违反我们的分类法。因此,改为重定向到 fruit.php 您实际上可以将其重定向到真正的模板 taxonomy-fruits.php 并对其进行修改。
    • 当然你的解决方案很有趣。当我这么说时,我只是指当您尝试访问 domain.com/_cpt_/_taxonomy_/ URL 并获得 404 代码响应时的情况。
    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 2014-10-10
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2010-10-17
    相关资源
    最近更新 更多