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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类