[Resolved] CPT category archive page redirections to WP Show Posts page

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support CPT category archive page redirections to WP Show Posts page

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #23812
    Daniel
    Participant

    Hi!

    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)?.

    #23906
    elvin
    Moderator

    Hi,

    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-18007

    This 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.

    #24046
    Daniel
    Participant

    Thanks 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');
    • This reply was modified 4 months, 4 weeks ago by Daniel.
    #24109
    elvin
    Moderator

    Could 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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.