Why are my hits for a taxonomy term page getting a 404 error?
I have taxonomy "source', registered in code through a plugin...
<?php
/**
* Plugin Name: Context Taxonomy for Sources
* Description: Register custom taxonomy for sources.
* Version: 1.0.0
* Author: Robert Andrews
*/
/**
* ==============================================================================
* REGISTER TAXONOMY
* ==============================================================================
*/
if ( ! function_exists( 'register_taxonomy_source' ) ) {
function register_taxonomy_source() {
$labels = array(
'name' => _x( 'Sources', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Source', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Sources', 'text_domain' ),
'all_items' => __( 'All Sources', 'text_domain' ),
'parent_item' => __( 'Parent Source', 'text_domain' ),
'parent_item_colon' => __( 'Parent Source:', 'text_domain' ),
'new_item_name' => __( 'New Source Name', 'text_domain' ),
'add_new_item' => __( 'Add New Source', 'text_domain' ),
'edit_item' => __( 'Edit Source', 'text_domain' ),
'update_item' => __( 'Update Source', 'text_domain' ),
'view_item' => __( 'View Source', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate Sources with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove Source', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Source', 'text_domain' ),
'search_items' => __( 'Search Source', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No Source', 'text_domain' ),
'items_list' => __( 'Sources list', 'text_domain' ),
'items_list_navigation' => __( 'Sources list navigation', 'text_domain' ),
);
$rewrite = array(
'slug' => 'source',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'single_value' => false, // Use single-select radio button, only one Source per object
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy(
/* taxonomy name */ 'source',
/* attach to object */ array( 'quote','article','report','event', 'post' ),
/* arguments */ $args
);
}
add_action( 'init', 'register_taxonomy_source', 0 );
}
?>
The taxonomy works in the back-end.
Now I am building out a theme, I want to see that a page like http://www.example.com/source/term-name works. But it doesn't.
My .htaccess file contained...
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
# Modified for Multisite https://www.namecheap.com/support/knowledgebase/article.aspx/9191/29/how-to-create-a-wildcard-subdomain-in-cpanel
# Previous here:
# RewriteEngine On
# RewriteBase /
# RewriteRule ^index\.php$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
I have just removed those parts. Before this, the 404 /source/term-name page was actually getting redirected to the homepage.
I have...
- Removed the above .htaccess portions.
- Clicked Save on the Permalinks page
- Added
flush_rewrite_rules()
and similar to my code.
Still it's coming up 404. I don't understand it.
One interesting thing to note... On my "Source" taxonomy listing in the back-end, when you open a taxonomy term page with the view link, I just saw each site URL is like http://www.example.com/blog/source/term-name - not http://www.example.com/source/term-name. It should be the latter. However, both variants fail 404.
On my Permalinks Settings page, "Common Settings" is set to "Custom Structure": /blog/%year%/%monthnum%/%day%/%postname%/ That may be where "blog" is coming from, but I still don't understand the 404.
I just removed "/blog/". Now the URL is http://www.example.com/source/term-name - but, still, that is 404.
Use of undefined constant term_name - assumed 'term_name
'". Count: 1. Location:wp-content/themes/elegant/taxonomy-source.php:38
. Caller:wp-includes/template-loader.php wp-includes/template-loader.php:74
. Component:Core
. >>>>> Line 38 in taxonomy-source.php included "<?php echo term_name; ?>" - it was objecting toterm_name;
I'm changing to$term = get_queried_object();
followed by<?php echo $term->name; ?>
Thanks for the Query Monitor tip, I guess. it's like a Chrome Inspector for WordPress.