WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A plugin to redirect user if there's only 1 post in category/tag page ? (2 posts)

  1. riversatile
    Member
    Posted 2 years ago #

    Hi,

    Is there a plugin that allows me to redirect users if there's only 1 post in category/tag page ? I mean redirect them directly to this single post.

  2. riversatile
    Member
    Posted 2 years ago #

    Sure, but there is a better way to do that !
    Simply paste this into your functions.php file (located in your theme folder)

    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');

    Then all the users will be redirected to the single post found in any category/tag page ;)

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic