Home › Forums › Pro Support › CPT category archive page redirections to WP Show Posts page
Tagged: category pages, redirection
- This topic has 3 replies, 2 voices, and was last updated 2 years, 9 months ago by
elvin.
-
AuthorPosts
-
December 5, 2020 at 3:36 am #23812
Daniel
ParticipantHi!
I need to redirect the category pages to a static page, as I have seen here: https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/ using this code:
<?php add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } if ( isset( $query_vars['category_name'] ) ) { $pagename = $query_vars['category_name']; $query_vars = array( 'pagename' => "$pagename" ); } return $query_vars; } ); ?>
To add the code, I used the hook wp_head in elements with the option to run php marked and displayed throughout the whole site.
It should redirect from this archive page: https://tasteofcanadaeurope.com/category/events/
to this static page: https://tasteofcanadaeurope.com/events/But I can’t get it to work. T tried changing ‘category_name’ to ‘events’ in the snippet but it didn’t work either.
Also, could I use that code with a custom post type and custom taxonomies to make the taxonomy archive pages redirect all to the same page (the page where I’m using WP SHow Posts)?.
December 6, 2020 at 9:30 pm #23906elvin
ModeratorHi,
To clarify: You’re trying to make archive pages that lists posts using WPSP?
While this isn’t exactly related to the approach you’re trying to do, perhaps you can try or give it some thought.
https://wpshowposts.com/support/topic/use-wp-show-posts-to-replace-blog-archive-page/#post-18007This does a completely different approach to doing the same thing. It may be tedious or intimidating to do but it is better than redirecting because redirections are generally bad user experience.
December 9, 2020 at 10:38 am #24046Daniel
ParticipantThanks Elvin,
I couldn’t use the solution you gave me because in addition to redirecting the archive page I also needed to redirect the posts within the events category to the page with Wp Show Posts.
I had tried several snippets and none of them worked. Now I have seen that instead of using the elements section inside GP, if I use the Code Snippets plugin the redirections worked correctly.
Could it be that when using the hook wp_head inside elements they didn’t work for that reason?
This is the snippet that worked for me using Code Snippets in case it can be helpful for someone else:
function redirect_events_posts_and_category() { if ( (is_category( 'events' ) || in_category('events'))) { wp_redirect( site_url( 'events/' ), 301 ); die; } } add_action( 'template_redirect', 'redirect_events_posts_and_category');
December 10, 2020 at 8:50 pm #24109elvin
ModeratorCould it be that when using the hook wp_head inside elements they didn’t work for that reason?
That’s it. GP’s Hook Element was meant for markups you want to hook on specific parts of the page.
For PHP snippets that filters things you’ll have to either use Code Snippets plugin or a Child Theme’s
functions.php
.Nice one! Thank you for sharing.
-
AuthorPosts
- You must be logged in to reply to this topic.