du4010 2014-08-27 00:48
浏览 36
已采纳

WP Query对短代码进行分页

I am creating a shortcode to display the most recent posts in the homepage, however I cannot get the pagination working.

When you click on 'older posts' the page refreshes but the same 2 original posts are displayed.

if ( ! function_exists('vpsa_posts_shortcode') ) {
    function vpsa_posts_shortcode( $atts ){

        $atts = shortcode_atts( array(
                        'per_page'  =>      2,  
                        'order'     =>  'DESC',
                        'orderby'   =>  'date'
                ), $atts );

        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

        $args = array(
            'post_type'         =>  'post',
            'posts_per_page'    =>  $atts["per_page"], 
            'order'             =>  $atts["order"],
            'orderby'           =>  $atts["orderby"],
            'paged'             =>  $paged
        );

        $query = new WP_Query($args);

                if($query->have_posts()) : $output;

                    while ($query->have_posts()) : $query->the_post();

                        $output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';

                            $output .= '<h4 class="post-title"><span><a href="' . get_permalink() . '" title="' . the_title('','',false) . '">' . the_title('','',false) . '</a></span></h4>';

                            $output .= '<div class="row">';

                                $output .= '<div class="col-md-3">'; 

                                    $output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';

                                        if ( has_post_thumbnail() ) {

                                            $output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));

                                        } else {

                                           $output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';                                           

                                        }

                                    $output .= '</a>';

                                $output .= '</div>';

                                $output .= '<div class="col-md-9">';

                                    $output .= get_the_excerpt();

                                    $output .= '<span class="post-permalink"><a href="' . get_permalink() . '" title="' . the_title('','',false) . '">Read More</a></span>';

                                $output .= '</div>';

                            $output .= '</div>';

                            $output .= '<div class="post-info">';

                                $output .= '<ul>';

                                    $output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';

                                    $output .= '<li>By: ' . get_the_author() . '</li>';

                                    $output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';

                                $output .= '</ul>';

                            $output .= '</div>';

                        $output .= '</article>';

                    endwhile;

                    $output .= '<div class="post-nav">';

                        $output .= '<div class="prev-page">' . get_previous_posts_link( "« Newer Entries" ) . '</div>';

                        $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';

                    $output .= '</div>';

                else:

                    $output .= '<p>Sorry, there are no posts to display</p>';

                endif;

            wp_reset_postdata();

            return $output;
    }
}

add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');
  • 写回答

1条回答 默认 最新

  • douqiu0796 2014-08-27 06:10
    关注

    You need to call paginate function.Try following code:

    if ( ! function_exists('vpsa_posts_shortcode') ) {
            function vpsa_posts_shortcode( $atts ){
    
                $atts = shortcode_atts( array(
                                'per_page'  =>      2,  
                                'order'     =>  'DESC',
                                'orderby'   =>  'date'
                        ), $atts );
    
                $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
                $args = array(
                    'post_type'         =>  'post',
                    'posts_per_page'    =>  $atts["per_page"], 
                    'order'             =>  $atts["order"],
                    'orderby'           =>  $atts["orderby"],
                    'paged'             =>  $paged
                );
    
                $query = new WP_Query($args);
                        if($query->have_posts()) : $output;
    
                            while ($query->have_posts()) : $query->the_post();
    
                                $output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';
    
                                    $output .= '<h4 class="post-title"><span><a href="' . get_permalink() . '" title="' . the_title('','',false) . '">' . the_title('','',false) . '</a></span></h4>';
    
                                    $output .= '<div class="row">';
    
                                        $output .= '<div class="col-md-3">'; 
    
                                            $output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';
    
                                                if ( has_post_thumbnail() ) {
    
                                                    $output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));
    
                                                } else {
    
                                                   $output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';                                           
    
                                                }
    
                                            $output .= '</a>';
    
                                        $output .= '</div>';
    
                                        $output .= '<div class="col-md-9">';
    
                                            $output .= get_the_excerpt();
    
                                            $output .= '<span class="post-permalink"><a href="' . get_permalink() . '" title="' . the_title('','',false) . '">Read More</a></span>';
    
                                        $output .= '</div>';
    
                                    $output .= '</div>';
    
                                    $output .= '<div class="post-info">';
    
                                        $output .= '<ul>';
    
                                            $output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';
    
                                            $output .= '<li>By: ' . get_the_author() . '</li>';
    
                                            $output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';
    
                                        $output .= '</ul>';
    
                                    $output .= '</div>';
    
                                $output .= '</article>';
    
                            endwhile;global $wp_query;
        $args_pagi = array(
                'base' => add_query_arg( 'paged', '%#%' ),
                'total' => $query->max_num_pages,
                'current' => $paged
                );
                            $output .= '<div class="post-nav">';
                                $output .= paginate_links( $args_pagi);
    
                            //    $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
    
                            $output .= '</div>';
    
                        else:
    
                            $output .= '<p>Sorry, there are no posts to display</p>';
    
                        endif;
    
                    wp_reset_postdata();
    
                    return $output;
            }
        }
    
        add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化