Support » Themes and Templates » Redirect category to first post

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey Orkhan. Go to your functions.php and paste this code there:

    add_action('get_header','redirecttofirstpost');
    function redirecttofirstpost(){
    if (is_category()){
    $cur_cat_id = get_cat_id( single_cat_title("",false) );
    $args = array( 'numberposts' => 1, 'category' =>$cur_cat_id );
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) :
    $pid=$post->ID;
     endforeach;
    wp_redirect( home_url('/?p='.$pid) );
    exit;
    }
    }

    For spesific category

    add_action('get_header','redirecttofirstpost');
    function redirecttofirstpost(){
    if (is_category()){
    $cur_cat_id = get_cat_id( single_cat_title("",false) );
    if ($cur_cat_id==YOUR_SPESIFIC_CATEGORY_ID) {
    $args = array( 'numberposts' => 1, 'category' =>$cur_cat_id );
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) :
    $pid=$post->ID;
     endforeach;
    wp_redirect( home_url('/?p='.$pid) );
    exit;
      }
     }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect category to first post’ is closed to new replies.