使用WP_QUERY添加带有分页的博客档案

时间:2016-07-19 作者:nsilva

我有以下代码:-

<!-- News Archive -->
<div id="news-archive">
    <div class="row">
        <div class="col-md-12"> 
        <?php
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $args = array(
          \'post_type\'       => \'post\',
          \'orderby\'         => \'title\',
          \'order\'           => \'ASC\',
          \'cat\'             => \'8\',
          \'offset\'          => 2,
          \'posts_per_page\'  => 6,
          \'paged\'           => $paged
        );
        $loop = new WP_Query($args);
        $post_counter = 1;
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-xs-12 col-sm-4 col-md-4">
                <div id="blog-<?php echo $post_counter; ?>" class="blog-wrapper">
                    <?php if(get_the_post_thumbnail()) {
                        the_post_thumbnail();
                    } else {
                    $category = "/wp-content/themes/irongate/assets/img/" . get_field(\'group_category\') . \'-default.jpg\'; ?>
                        <img src="<?php echo $category; ?>" />      
                    <?php } ?>
                    <span class="news-archive-date"><?php echo get_the_date(\'d M Y\'); ?></span>
                    <p class="news-archive-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                    <span id="<?php echo $case_category; ?>-logo" class="<?php echo get_field(\'group_category\') == \'inone\' ? \'inone-catergory\' : \'news-archive-catergory\';?>"></span>
                    <a href="<?php the_permalink(); ?>">
                        <div class="news-overlay blog-overlay">
                            <span class="news-overlay-excerpt"><?php echo substr(get_the_excerpt(), 0,240); ?>...</span>
                            <span id="careers-overlay-more" class="btn-white">Read more</span>
                        </div>
                    </a>                
                </div>
            </div>
            <?php if($post_counter % 3 == 0) {echo \'<div class="clearfix"></div>\';} 
        $post_counter++;  
        endwhile; ?><!-- News Archive -->
        <div class="navigation">
            <div class="alignleft"><?php previous_posts_link(\'&laquo; Previous\') ?></div>
            <div class="alignright"><?php next_posts_link(\'More &raquo;\') ?></div>
        </div>
        <?php wp_reset_postdata(); ?>
        </div>
    </div>
</div>
帖子的显示正确显示为6篇博客帖子和“更多…”按钮出现,但单击时只会重新加载相同的内容。

如何将分页添加到博客帖子?i、 e.单击下一步。。。将显示帖子9-14,而不是2-8(因为我使用的偏移量是2)。

提前感谢!

3 个回复
最合适的回答,由SO网友:dan9vu 整理而成

您可以计算offset 通过pagedposts_per_page. E、 g:

$per_page = 6;
$paged    = get_query_var(\'paged\') ? : 1;
$offset   = (1 === $paged) ? 0 : (($paged - 1) * $per_page) + (($paged - 1) * 2);

$args = array(
  \'order\' => \'ASC\',
  \'paged\' => $paged,
  \'offset\' => $offset,
  \'orderby\' => \'ID\',
  \'post_type\' => \'post\',
  \'post_status\' => \'publish\',
  \'posts_per_page\' => $per_page,
  \'ignore_sticky_posts\' => 1
);

$query = new WP_Query($args);

while ( $query->have_posts() ) : $query->the_post();
  echo get_the_title() . \'<br>\';
endwhile;

previous_posts_link(\'&laquo; Previous\', $query->max_num_pages);
if ($paged > 1) echo \' | \';
next_posts_link(\'More &raquo;\', $query->max_num_pages);

echo \'<br> Showing \' . $offset . \'-\' . ($offset + 6) . \' of \' . $query->found_posts . \' posts.\';

wp_reset_postdata();
请注意,循环从索引0开始,因此如果我们忽略粘性帖子,并且每页仅显示6篇帖子,那么第2页上的结果应该是8-14,而不是您所期望的9-14。

SO网友:mlimon

如果您正在使用next_posts_link() &&;previous_posts_link() 在自定义查询中,您必须传递$max\\u pages参数。喜欢->

previous_posts_link( \'&laquo; Previous\', $loop->max_num_pages );
next_posts_link( \'&laquo; Previous\', $loop->max_num_pages );
或者如果您只想使用previous\\u posts\\u链接(“previous”);如果不使用$max\\u pages参数,则必须使用默认查询而不是自定义查询。最好使用默认值。查看更多详细信息When to use WP_query(), query_posts() and pre_get_posts

SO网友:CK MacLeod

也可以使用paginate\\u链接:

//First get a count of how many pages you\'ll be producing:

$cat_id = 8;
$per_page = 6;

$all_posts = get_term_by( \'id\', $cat_id, \'category\' );
$posts_count = $all_posts->count ;
$total_pages = intval( $posts_count / $per_page ) + 1;

$current_page = max(1, get_query_var( \'paged\' ) );

echo \'<div class="pagination">\' ;

echo paginate_links(array(
    //several different ways to do the base variable in my observation/experience
    \'base\' => get_pagenum_link(1) . \'%_%\',
    \'current\' => $current_page,
    \'total\' => $total_pages,
    //if you like the double arrow (Euro quotes) format
    \'prev_text\' => \'&laquo; Previous\',
    \'next_text\' => \'Next &raquo;\' 
) );

echo \'</div>\' ;
根据您的permalinks样式,可能需要一些变化,并且您可以根据可用的参数和标准或自定义输出为链接本身找到许多有趣的格式,但我认为这应该回答了基本问题。看见ttps://codex.wordpress.org/Function_Reference/paginate_links对于其他基本示例。

相关推荐

GET_POSTS查询大约需要40秒来执行

我在get\\u帖子中有一个元查询,它需要花很长时间才能完成。它工作得很好,但只是时间太长了。我有一个名为event. 在每个event 发布后,有自定义元数据:post\\U sort\\U日期(事件日期YmdHis 格式,用于排序)我需要做的是获取下一个事件,该事件相对于$year 和$month 变量。所以如果$year = 2021 和$month = 10 (2021 10月)然后应该在2021 11月或之后找到第一个事件。我下面的查询很好,但很慢。执行大约需要40秒,我不知道为什么。$next