简码中的WordPress分页功能总是显示在顶部


WordPress pagination fuction in shortcode always displayed on the top

WordPress在短代码中的分页功能总是显示在顶部。请看下面的代码

/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
    $showitems = ( $range * 2 ) + 1;  
    global $paged;
    if(empty($paged)) 
        $paged = 1;
    if($pages == ''){
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if(!$pages){
            $pages = 1;
        }
    }
    if( 1 != $pages ){
        $pagination_html .= '<div class="pagination">';
        if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
            $pagination_html .= '<a href="' . get_pagenum_link( 1 ) . '">&laquo;</a>';
        }
        if( $paged > 1 && $showitems < $pages ){
            $pagination_html .= '<a href="' . get_pagenum_link( $paged - 1 ) . '">&lsaquo;</a>';
        }
        for ( $i = 1; $i <= $pages; $i++ ){
            if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
                if ( $paged == $i ){
                    $pagination_html .= '<span class="current">' . $i . '</span>';
                } else{
                    $pagination_html .= '<a href="' . get_pagenum_link( $i ). '" class="inactive">' . $i . '</a>';
                }
            }
        }
        if ( $paged < $pages && $showitems < $pages ){
            $pagination_html .= '<a href="' . get_pagenum_link( $paged + 1 ) . '">&rsaquo;</a>';
        }
        if ( $paged < $pages - 1 &&  $paged + $range - 1 < $pages && $showitems < $pages ){
            $pagination_html .= '<a href="' . get_pagenum_link( $pages ) . '">&raquo;</a>';
        }
        $pagination_html .= '</div>';
        return $pagination_html;
    }
}

然后我在shortcode函数中调用了它。

/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
    extract(shortcode_atts(array(
        "comments" => 'true',
        "date" => 'true',
        "columns" => '4',
        "limit" => '-1',
        "title" => 'true',
        "description" => 'true',
        "cat_slug" => '',
        "post_type" => '',
        "excerpt_length" => '15',
        "readmore_text" => '',
        "pagination" => 'false'
    ), $atts));
    global $post;
    $postformat = get_post_format();
    ....
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
    ......
    if (have_posts()) : while (have_posts()) : the_post();
        $postformat = get_post_format();
            if( $postformat == "" ) $postformat="standard";
        $protected = "";
        $portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
            .....////
            $portfoliogrid .= '<div class="summary-info">';
                .......
            $portfoliogrid .='</div>';
            // If either of title and description needs to be displayed.
            if ( $title == "true" || $description == "true" ) {
                $portfoliogrid .='<div class="work-details">';
                ......
                $portfoliogrid .='</div>';
            }
        $portfoliogrid .='</div>';
    endwhile; endif;
    if ( $pagination == "true" ){
        if ( isset( $additional_loop ) ){
            echo suareztheme_pagination( $additional_loop->max_num_pages );
        } else {
            echo suareztheme_pagination();
        }
        if ( function_exists("suareztheme_pagination") ) {
        } else {
            next_posts_link('&laquo;&laquo; Older Posts');
            previous_posts_link('Newer Posts &raquo;&raquo;');
        }
    }
    wp_reset_query();
    return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );

上面的代码运行良好,但有一个问题,当它显示在特定页面中时,无论它在哪里,它总是显示在顶部。非常感谢您的支持和帮助,提前表示感谢。

在shortcode回调中,您需要连接并返回html,例如:

$portfoliogrid .= suareztheme_pagination();

请注意,您必须使用get_next_posts_link()get_previous_posts_link(),而不是next_posts_link()previous_posts_link()