短代码中的WordPress分页功能始终显示在顶部

时间:2015-03-22 18:00:46

标签: php wordpress function pagination shortcode

短代码中的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;
    }

}

然后我在短代​​码函数中调用它。

/*-------------------------------------------------------------------------*/
/* 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" );

上面的代码工作正常,但它有一个问题,当它显示在特定页面时,它总是显示在顶部而不管它的位置。感谢您的支持,帮助我,提前谢谢。

1 个答案:

答案 0 :(得分:0)

在短代码回调中,你需要连接并返回html,例如:

$portfoliogrid .= suareztheme_pagination();

请注意,您必须使用get_next_posts_link()get_previous_posts_link()代替next_posts_link()previous_posts_link()