自定义POST循环的分页

时间:2020-05-26 作者:Kertix

我做了一些短代码,将数据插入主屏幕。我循环查看帖子总数并输出预先设置好样式的html。

我的问题是,是否也可以对这种循环进行分页?例如,如果我有10篇帖子,我想显示5篇,然后在这些帖子下面是另一篇5的链接。或者如果这不可能,我应该朝哪个方向看?

目前,我正在显示这样的数据(这只是标题,因为我把它整理得更短,以便放在这里):

    add_shortcode(\'title-snippet\', \'wpc_shortcode_title\');
    function wpc_shortcode_title() {
        $args = array( \'numberposts\' => \'1000\' );
        $recent_posts = wp_get_recent_posts( $args );

        $html = \'<div></div>\';

        for($x = 0; $x < count($recent_posts); $x++) {
            $html .= \'
                <div class="vce-text-block">
                    <div class="vce-text-block-wrapper vce">
                        <p>
                            <span style="color: #ffffff; font-family: Playfair Display;">
                                <span style="font-size: 21.3333px;">
                                    \'.$recent_posts[$x]["post_title"].\'
                                </span>
                            </span>
                        </p>
                    </div>
                </div>
            \';
        }

        return <<<HTML
            $html
        HTML;
    }

2 个回复
最合适的回答,由SO网友:Yash Tiwari 整理而成

在短代码中使用分页的Post循环[title-snippet type="post" limit="5"]:

function wpc_shortcode_title( $atts ) {
extract( shortcode_atts( array( \'limit\' => 5, \'type\' => \'post\'), $atts ) );

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

query_posts(  array ( 
    \'posts_per_page\' => $limit, 
    \'post_type\' => $type, 
    \'order\' => \'ASC\', 
    \'orderby\' =>\'menu_order\', 
    \'paged\' => $paged ) );

$list = \' \';   

while ( have_posts() ) { the_post();

    $list .= \'<article class="listing-view clearfix">\' 
    . \'<div class="listing-content">\' 
    . \'<h3><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></h3>\' 
    .\'<p>\' . get_the_excerpt() . \'</p>\'
    . \'<a href="\' . get_permalink() . \'">\' . \'View &raquo;\' . \'</a>\'
    . \'</div>\'
    . \'<a class="listing-thumb" href="\' . get_permalink() . \'">\' . get_the_post_thumbnail($page->ID, \'listing-thumb\')  . \'<span></span></a>\'
    . \'</article>\';
}

return 
\'<div class="listings clearfix">\' 
. $list 
. \'<div class="nav-previous">\' . get_next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\' ) ) . \'</div>\'
. \'<div class="nav-next">\' . get_previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\' ) ) . \'</div>\'
. \'</div>\' .
wp_reset_query();

}
add_shortcode( \'title-snippet\', \'wpc_shortcode_title\' );

SO网友:Yash Tiwari

是的,可以显示数字分页。请尝试以下更新的代码:

function wpc_shortcode_title( $atts ) {
extract( shortcode_atts( array( \'limit\' => 5, \'type\' => \'post\'), $atts ) );

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

query_posts(  array ( 
    \'posts_per_page\' => $limit, 
    \'post_type\' => $type, 
    \'order\' => \'ASC\', 
    \'orderby\' =>\'menu_order\', 
    \'paged\' => $paged ) );

$list = \' \';   

while ( have_posts() ) { the_post();

    $list .= \'<article class="listing-view clearfix">\' 
    . \'<div class="listing-content">\' 
    . \'<h3><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></h3>\' 
    .\'<p>\' . get_the_excerpt() . \'</p>\'
    . \'<a href="\' . get_permalink() . \'">\' . \'View &raquo;\' . \'</a>\'
    . \'</div>\'
    . \'<a class="listing-thumb" href="\' . get_permalink() . \'">\' . get_the_post_thumbnail($page->ID, \'listing-thumb\')  . \'<span></span></a>\'
    . \'</article>\';
}

    global $wp_query;
    $args_pagination = array(
            \'base\' => add_query_arg( \'paged\', \'%#%\' ),
            \'total\' => $wp_query->max_num_pages,
            \'current\' => $paged
            );
    $list .= \'<div class="post-nav">\';
        $list .= paginate_links( $args_pagination);
    $list .= \'</div>\';

return 
\'<div class="listings clearfix">\' 
. $list 
. \'</div>\' .
wp_reset_query();

}
add_shortcode( \'title-snippet\', \'wpc_shortcode_title\' );

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?