Why am I getting a 404 error when trying to reach the taxonomy.php page without any terms, (Wordpress don't see taxonomy page)

一曲冷凌霜 提交于 2021-02-05 07:09:23

问题


I'm trying to create taxonomy page so I created taxonomy with taxonomie taxonomy-nowe.php

but my WP don't see the page or I mess something with the rewrite URL? Can someone check my code and see if something I'm doing wrong. I did the flush the permalinks by save the Plain and then return to Post name permalinks. But I get not result.

I want to achive somethink like this: 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 );

回答1:


This post has been edited many times as, at the time, I didn't had all the information regarding the issue. I've since made extensive research on that subject. The following goes straight to the point and the answer.


We have two goals here, getting the permalink structure to the following
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...

And get the taxonomy.php to return an index of posts and terms associated to a specific taxonomy part a of custom post type instead of a 404.php page.


"When a visitor clicks on a hyperlink to category, tag or custom taxonomy, WordPress displays a page of posts in reverse chronological order filtered by that taxonomy."

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

The default template used for taxonomies is taxonomy.php. These files let you target specific taxonomies or specific taxonomy terms. For example: taxonomy-{taxonomy}-{term}.php and taxonomy-{taxonomy}.php

In your case when you search for ./fruits/apples you get all posts related to the term apples either displayed on either taxonomy.php or on taxonomy-fruits.php or finally on taxonomy-fruits-aples.php. But what if you want to access ./fruits/? Why does accessing fruit gives back a 404.php error? Well because taxonomy.php was intended to displays posts against terms, not posts against taxonomies. This is the normal and intended behaviour.

This goes back to 10 years ago https://core.trac.wordpress.org/ticket/13816, and multiple issues were raised. (Interesting read, you should take a look).

When you search for ./fruits/apples/ we get a "true" but with ./fruits/ we get a "false" with that logic in mind if a user search for ./fruits/ or ./fruits he should be redirected to a template with all related fruits posts or terms, as this is not default behaviour, I came up with a case by case solution:

<?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();
  };
}; ?>

Regarding substr, value should match the number of characters your taxonomy slug is equal to WITH dashes. substr( $url, -7 ) == '/fruits'

This is probably not the best way but it does the job and it is pretty quick. I didn't see any other solution that was working (September 2020).

Where fruits.php is your fallback template in case a user search for ./fruits/ or ./fruits. We take the end of the url to match it against our custom taxonomy slug. This should only happens when is_tax() is false ! is_tax( 'fruits' ) and the url matches our taxonomy.

Let's say now that our custom taxonomy is associated with a custom post type called "recipes". We can then use the following to display posts and associated terms for a custom post type on our fallback fruit.php template file:

<?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;
};

}; ?>

Additionally you also wanted to have the custom post type (CPT) for your custom taxonomy, in your permalink, in front of your taxonomy, to get a structure like so: recipes/fruits/apples where recipes is your custom post type. You can use the rewrite rule upon registering that taxonomy's arguments using the following: 'rewrite' => array( 'slug' => 'recipes/fruits' ), hierarchical => 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 );
};

Our permalink structure should now look like the following
domain.com/recipes/fruits/apples/ ...
domain.com/_custom_post_type_/_taxonomy_/_term_/ ...

The taxonomy page can now be use as a posts and a taxonomies index.



来源:https://stackoverflow.com/questions/63907037/why-am-i-getting-a-404-error-when-trying-to-reach-the-taxonomy-php-page-without

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!