无法使WordPress插件正确分页

时间:2013-07-19 17:35:25

标签: php wordpress-plugin wordpress

我正在尝试为WordPress构建一个插件。目标是使用我想要!--nextpage-->标签的短代码(分页),然后常规访问者(未登录)在我的帖子中有分页,但对于所有登录的访问者,请使用邮件继续前进而没有看到分页(鼓励人们成为会员)。

问题是,我可以让它不显示登录成员的任何内容,但它加载页面而不使用此代码为未登录的访问者添加分页。因此,他们仍然只看到中间的链接而没有分页的整个内容。

我需要调整什么来使其正确加载?

function pagination_show( $atts = null ) {

    extract( shortcode_atts( array(
        'nextpage_text' => 'CONTINUED ON NEXT PAGE',
        'previous_text' => 'CONTINUED FROM PREVIOUS PAGE',
    ), $atts ) );
    $pagination = '';

    if(!is_user_logged_in()){
        $pagination = '<div class="plugin-pagination" style="overflow:auto;">';

        $next_post = get_next_post();
        if (!empty( $next_post )): 
            $pagination .= '<strong style="float:left;" class="nextpage"><em><a href="'.get_permalink( $next_post->ID ).'">'. esc_attr($nextpage_text) .'</a></strong></em><!--nextpage-->';
        endif;

        $prev_post = get_previous_post();
        if (!empty( $prev_post )): 
            $pagination .= '<strong style="float:right;" class="previouspage"><em><a href="'.get_permalink( $prev_post->ID ).'">'. esc_attr($previous_text) .'</a></strong></em><!--previouspage-->';
        endif;

        $pagination .= '</div>';
    }
    return  $pagination ;
}

add_shortcode('pagination', 'pagination_show');

1 个答案:

答案 0 :(得分:0)

您应该使用wp_link_pages进行分页。

对于登录用户,使用the_content过滤器显示没有分页的内容。

例如,在你的post.php(或任何模板)文件中

if( is_user_logged_in() ) :
  echo apply_filters( 'the_content', $post->post_content );
else :
  the_content();
  wp_link_pages( $my_args );
endif;