WP偏移不适用于Foreach循环

时间:2017-08-01 20:46:17

标签: php wordpress foreach offset categories

我目前正在使用wordpress循环来检索博客帖子,标题,特色图片,日期和类别。话虽如此,我试图在第5个降序帖子上偏移循环开始,因为前面的4个在页面上被引用。

我已经成功抵消了帖子,但似乎我无法抓住这个类别。

<?php
    $post_args = array(
                 'post_type'   => 'post',
                 'post_status' => 'publish',
                 'order'       => 'DESC',
                 'offset'      => 4
                );
    $post_query = new WP_Query($post_args);
    if ($post_query->have_posts() ):
    $count = 1;
    $terms = get_terms( array(
                    'taxonomy'   => 'category',
                    'hide_empty' => true
             ) );
    while ( $post_query->have_posts() ) : $post_query->the_post();
    $feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="col-sm-3 col-xs-6">
  <div class="featured-img" style="background-image: url(<?php echo $feat_img; ?>)"
    <?php the_date('F j Y', '<h6>', '</h6>'); ?>
    <h3><?php the_title(); ?></h3>
    <div class="category"><?php echo $terms->name; ?></div>
  </div>
</div>

我尝试了一种稍微不同的方法,并且能够使用foreach循环获得每个帖子类别,然后是while和if循环。虽然我成功获得了每个职位类别,但抵消不合作。也许我正在思考它。这是我对此的另一次尝试。

<?php
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => true,
    ) );
    $count = 1;
    foreach ( $terms as $term ) :
    $post_args = array(
        'offset' => 4,
        'post_type' => 'post',
        'order' => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $term->slug  
            )
        ),  
    );
    $post_query = null;
    $post_query = new WP_Query($post_args);
    if ( $post_query->have_posts() ) :

    while ($post_query->have_posts() ) : $post_query->the_post();
    $feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>

任何人都想放心帮助完成这两项任务?任何投入将不胜感激。先感谢您。

1 个答案:

答案 0 :(得分:0)

你需要将“posts_per_page”设置为除-1之外的其他值,这在文档中有很好的解释

https://codex.wordpress.org/Class_Reference/WP_Query

  

posts_per_page(int) - 每页显示的帖子数量(可用   从版本2.1开始,取代了showposts参数)。使用   'posts_per_page'=&gt; -1显示所有帖子('offset'参数为   用-1值忽略)。如果分页,则设置'paged'参数   使用此参数后关闭。注意:如果查询位于Feed中,   wordpress使用存储的'posts_per_rss'覆盖此参数   选项。要重新实现限制,请尝试使用'post_limits'过滤器,或   过滤'pre_option_posts_per_rss'并返回-1

相关问题