WP Query对短代码进行分页

时间:2014-08-27 00:48:17

标签: php wordpress pagination

我正在创建一个短代码来显示主页中的最新帖子,但我无法让分页工作。

当您点击“旧帖子”时页面刷新但显示相同的2个原始帖子。

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');

2 个答案:

答案 0 :(得分:1)

您需要调用paginate函数。请尝试以下代码:

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');

答案 1 :(得分:0)

WP_Query 在短代码中带有分页

在这里,我将向您展示如何创建一个 [shortcode],其中包含带有分页链接的自定义新 WP_Query

使用新的 WP_Query 帖子循环创建 [shortcode] 并内置分页链接,以将任何 is_singular() 页面或单个帖子转换为基于以下内容的自定义存档您设置的 WP_Query 参数。让我们称之为[wp_query_pagination_inside_shortcode],所以我们在functions.php中添加以下add_shortcode()

或者在我的情况下,我创建单独的 /functions/ 文件夹用于文件以使用适当的名称保持井井有条,并有一个 require_once 包含文件结构。

root/functions.php ->

require_once 'functions/shortcode-wp-query-pagination-inside-shortcode.php';

root/functions/shortcode-wp-query-pagination-inside-shortcode.php->

add_shortcode('wp_query_pagination_inside_shortcode', 'my_shortcode_function_tag');
if (!function_exists('my_shortcode_function_tag')) {
    function my_shortcode_function_tag($atts)
    {
        ob_start(); // Turn output buffering on, helps with complicated template and theme structures

        // Note: https://developer.wordpress.org/reference/functions/get_query_var/#used-by
        // Custom query_vars translates to Getting an "archive" on any current page that contains a query with corresponding pagination number in the url
        // example.com/page-shortcodes-on/page/2/  ...etc see below how to manipulate this
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $atts = shortcode_atts(
            array(
                'post_type' => 'post',
                // Set attributes here you want in your shortcode
                // like [shortcode post_types="post"]
            ),
            $atts
        );
        // set up the default args you wont change and the ones you will by accessing the value of $atts passed into the shortcode function from your WYSIWYG content editor
        $args = array(
            'posts_per_page' => 3,
            'paged' => $paged, // Important to receive page data
            'post_type' => $atts['post_type'], // This is how you get the values of your shortcode attributes passed in
            'orderby' => 'date',
            'order' => 'DESC',
        );
        $the_query = new WP_Query($args);
        // Da loop
        // match your themes loop structure this below is just boiler plate stuff ignore or use your choice its not important how you create the post loop, just do it
        if ($the_query->have_posts()) {
            while ($the_query->have_posts()) {
                $the_query->the_post();
                get_template_part('template-parts/loop');
            } //end while
        } // endif
        $big = 999999999; // need an unlikely integer for how many pages are possible
        // I've tested example.com/page-shortcodes-on/page/999999999999999999/ and the custom query_var still works
        echo paginate_links(
            array(
                'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), // referrence the url
                'format' => '?paged=%#%', // used for replacing the page number
                'current' => max(1, get_query_var('paged')), // grabs the page data
                'total' => $the_query->max_num_pages //$the_query is your custom query
            )
        );
        return ob_get_clean(); // Silently discard the buffer contents
        wp_reset_query(); // Lets go back to the main_query() after returning our buffered content
    }
}

在页面或帖子内容编辑器中的 WYSIWYG 内容编辑器

[wp_query_pagination_inside_shortcode post_type="posts"]

此查询的 URL 结构_var 'paged'

example.com/page-shortcode-is-on/page/2/

您需要对分页链接进行一些样式化以使其可接受,但这应该为您提供了一个非常有用的工具,可以通过 WYSIWYG 编辑器从您站点的任何位置构建具有多种不同 post_types 的自定义 WP_Query 循环。添加更多属性,让这个东西真正有价值。

// 下面忽略,这些只是针对此问题的可能搜索查询

WP_Query 内的简码分页 / add_shortcode 分页 / 短代码自定义属性 / 分页自定义帖子类型 wp_query 短代码 / 自定义帖子类型 wp_query 每页 / 页 / 分页存档帖子