admin管理员组文章数量:1201372
Please How do I achieve this?
I have WordPress Default Taxonomy - Tag and a custom Taxonomy - Artist.
I want to check for TAGS that have the same slug as ARTIST and 301 redirect it to the ARTIST.
Example Check If: website/tag/drake and website/artist/drake have the same slug which is true.
then
Automatically 301 Redirect: website/tag/drake to website/artist/drake
If the slug is not the same, then do nothing.
I found this Answer but it's not exactly what I need and I don't know how to make it solve my problem. I am very new to WordPress development.
Thanks
Please How do I achieve this?
I have WordPress Default Taxonomy - Tag and a custom Taxonomy - Artist.
I want to check for TAGS that have the same slug as ARTIST and 301 redirect it to the ARTIST.
Example Check If: website.com/tag/drake and website.com/artist/drake have the same slug which is true.
then
Automatically 301 Redirect: website.com/tag/drake to website.com/artist/drake
If the slug is not the same, then do nothing.
I found this Answer but it's not exactly what I need and I don't know how to make it solve my problem. I am very new to WordPress development.
Thanks
Share Improve this question asked Apr 11, 2022 at 20:49 Joe TitusJoe Titus 391 silver badge6 bronze badges1 Answer
Reset to default 1Try this, which should work, and if necessary, replace the artist
on the 5th line below with the correct taxonomy slug:
add_action( 'template_redirect', 'my_301_redirect_tag_to_artist' );
function my_301_redirect_tag_to_artist() {
if ( is_tag() ) {
// Check if an "artist" term exists with the same slug as the current tag.
$artist = term_exists( get_queried_object()->slug, 'artist' );
if ( is_array( $artist ) && isset( $artist['term_id'] ) ) {
$artist_id = (int) $artist['term_id']; // this ensures it's an integer
// Perform a safe 301 redirect to the "artist"/term archive page.
wp_safe_redirect( get_term_link( $artist_id ), 301 );
exit;
}
}
}
Things to note:
is_tag()
is one of the many conditional tags in WordPress, andis_tag()
checks if the current page is a tag archive, where "tag" here refers to the corepost_tag
taxonomy in WordPress. You can learn more about conditional tags here.term_exists()
checks if a specific term (tag, category or custom taxonomy's term) exists in the WordPress database, and basically returns an array containing the term ID if the term does exist.get_queried_object()
retrieves the data of the current term where the slug normally presents in the current URL as inhttps://example.com/tag/tag-slug/
.Or without permalinks or URL rewriting, the above URL might look like
https://example.com/?tag=tag-slug
instead.wp_safe_redirect()
, as the name implies, safely redirects to a local URL, i.e. a location at your WordPress website. And in my example, you could see that I explicitly set the status (the 2nd parameter) to 301 because the default one is 302.get_term_link()
retrieves the permalink/URL of a term, and in your case, the URL would look likehttps://example.com/artist/artist-slug/
.template_redirect
is an action hook which fires before WordPress determines and loads the template for the current request, and I used it because the documentation stated:This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
The last 10 words in the last sentence means, by the time the hook runs, WordPress has already setup the main query and determined what the request is for (e.g. a Page, a term archive, a search results page, etc.), and thus conditional tags would work as expected.
So I hope that helps, and be sure to visit the above links and understand those functions, instead of simply copy-pasting my code. :) Happy coding!
本文标签:
版权声明:本文标题:Check If Taxonomy A and Taxonomy B has same Slug, 301 Auto Redirect Tax A to Tax B if True in Wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738633112a2103867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论