我有一个让人们订阅的网站。我只想向写帖子的实际作者展示作者页面。我写了此代码,以检查发布问题的问题是我无法使用 wp_redirect 或包含使用它的模板,因为那时我会收到每个人的最爱“不能重新排列标头”消息。我可以显示“用户没有发布消息,但我认为将它们重定向到主页是一个更好的选择。

if ( is_author() ) : ?>

    <?php $id = get_query_var( 'author' );

    $post_count = get_usernumposts($id);
    if($post_count <= 0){ 
                 //This line could also be wp_redirect 
                 include( STYLESHEETPATH .'/author-redirect.php');
                 exit;
      }
endif;?>

谢谢

有帮助吗?

解决方案

您可以在更早的时刻通过挂接正确的动作来做到这一点 template_redirect, ,在显示模板之前发射。

add_action( 'template_redirect', 'wpse14047_template_redirect' );
function wpse14047_template_redirect()
{
    if ( is_author() ) {
        $id = get_query_var( 'author' );
        // get_usernumposts() is deprecated since 3.0
        $post_count = count_user_posts( $id );
        if ( $post_count <= 0 ) { 
            //This line could also be wp_redirect 
            include( STYLESHEETPATH .'/author-redirect.php' );
            exit;
        }
    }
}
许可以下: CC-BY-SA归因
scroll top