How to redirect to single post page if there is one post in category/tag

Community Forums Forums Design Tips and Tricks How to redirect to single post page if there is one post in category/tag

This topic is: resolved

This topic contains 3 replies, has 2 voices, and was last updated by  martialwarrior 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #93451

    martialwarrior
    Participant

    I want to know how to redirect to single post/page if there is one post in a category. I was told about this code snippet, put I am not sure where in the Functions PHP for the Blissful Theme the snippet should go, as when I put it where I think it should go, it breaks the site. Please advise exactly where to put the snippet. Thank you!

    function stf_redirect_to_post(){
    global $wp_query;

    // If there is one post on archive page
    if( is_archive() && $wp_query->post_count == 1 ){
    // Setup post data
    the_post();
    // Get permalink
    $post_url = get_permalink();
    // Redirect to post page
    wp_redirect( $post_url );
    }

    } add_action(‘template_redirect’, ‘stf_redirect_to_post’);

    http://usamahof.com/
    #93484

    braddalton
    Participant

    Please copy ALL the code from the view raw link and paste it at the end of your child themes functions.php file using a text editor like Notepad++

    https://gist.github.com/braddalton/7633999eb91c604b39db


    #148418

    martialwarrior
    Participant

    I am using the Whitespace Pro Theme on JohnNatividad.com and want redirect to single post page if there is one post in category/tag but when I use the code snippet below I get a redirect loop. What do I need to do to fix this?

    function redirect_to_single_post(){

    global $wp_query;

    if( is_archive() && $wp_query->post_count == 1 ):

    the_post();

    $post_url = get_permalink();

    wp_redirect( $post_url );

    endif;

    }
    add_action(‘template_redirect’, ‘redirect_to_single_post’);

    #148420

    martialwarrior
    Participant

    I got it fixed!!!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to redirect to single post page if there is one post in category/tag’ is closed to new replies.