【问题标题】:WordPress pagination fuction in shortcode always displayed on the top简码中的 WordPress 分页功能始终显示在顶部
【发布时间】:2015-03-23 02:07:10
【问题描述】:

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

上面的代码运行良好,但它有一个问题,当它显示在特定页面时,它总是显示在顶部,无论它在哪里。非常感谢您对我的支持,在此先感谢。

【问题讨论】:

    标签: php wordpress function pagination shortcode


    【解决方案1】:

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

    $portfoliogrid .= suareztheme_pagination();
    

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

    【讨论】:

    • 非常感谢@diggy。它现在工作得很好。还有一个问题,我什么时候应该使用 ob_start(),有 tuts 建议在函数之前和之后添加它。谢谢。
    • 不客气。无需使用输出缓冲,除非您无法返回所需的数据。
    • 嗨@diggy 我的问题仍然出现,除了顶部显示a 标签外,它运行良好,您可以在此处查看我的网站:128.199.180.6/demo/blog/blog-grid
    • 这是我这部分的代码:if ( $readmore_text != '' || $comments == "true" || $vote == "true" ) { $portfoliogrid .= '&lt;div class="entry-links"&gt;'; $portfoliogrid .= '&lt;div class="entry-links-left"&gt;'; $portfoliogrid .= dot_irecommendthis(); $portfoliogrid .= '&lt;span class="entry-comments"&gt;&lt;i class="fa fa-comments"&gt;&lt;/i&gt;' . comments_popup_link('Leave a Reply', '1', '%') . '&lt;/span&gt;'; $portfoliogrid .= '&lt;/div&gt;'; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多