Voulez-vous rediriger vos utilisateurs vers un article si une catégorie ou un tag qu’ils visitent ne contient que cet article ? C’est facile avec notre extrait de code. Dans cet article, nous allons vous montrer comment faire.
Instructions:
Tout ce que vous avez à faire est d’ajouter ce code dans le fichier functions.php de votre thème ou dans un plugin spécifique à votre site:
function redirect_to_post(){ global $wp_query; if( is_archive() && $wp_query->post_count == 1 ){ the_post(); $post_url = get_permalink(); wp_redirect( $post_url ); } } add_action('template_redirect', 'redirect_to_post');
Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez vous référer à notre guide sur comment copier/coller correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.
Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : Les meilleurs constructeurs de pages WordPress (comparés).
Thank you so much. It worked like wonder. Even expert could not helped me but this small codes did the charm. I am in love with this website. Bookmarked this website.
thank you so much.
is it possible to redirect 301?
not sure where on paste the code in function .php 🙁
HELP
Worked like a charm. Excellent and Thank you.
[…] A client requested to have a site visitor redirected to the single post if there is only one post in a custom taxonomy. Thanks to Kevin Chard over at WPSNIPP.COM I found this snippet which redirects user to the post page when the category or tag has only one post (original post). […]
great & Cool !!! thanks…
Glad you like the snippet enjoy Adm,
Your snippet is great !!!
Cool thanks Riversatile glad you like it.
Hey there Kevin, I am having a similar issue I believe. Big G indexed a bunch of category archives, now I have like 8 listings in the serps.. I was looking around on how to just redirect all categories to the homepage. Its a WP blog, and I read something about htaccss.
Is this similar to what you posted above?Thanks mateRu
Well the above script checks to see if a category has a post however you could simply check if it is an archive and then redirect to homepage.
function redirect_to_home(){
if( is_archive()){
wp_redirect(home_url());
}
} add_action(‘template_redirect’, ‘redirect_to_home’);
Thanks a lot Kevin! This works also for me. Was looking for this for a long time!
Mannn u save me (L), i was using a plugin to redirect, but is a lot of work put link by link to
redirect, and its shape redirects all at once, very good, and thank u
@google-bbb748471f941d6cd59d0e099de0729c:disqus cool glad that I could help out.