2017-08-01 120 views
1

我目前正在使用wordpress循环来检索博客文章,其标题,精选图​​像,日期和类别。这样说,我试图抵消循环从第5降序开始,因为前面的4在页面上更早引用。WP Offset不与Foreach循环工作

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

<?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循环获得每个帖子类别,接下来是一段时间,如果循环。虽然我成功获得了每个帖子类别,但抵消不合作。也许我正在推翻它。这是我的另一个尝试。

<?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()); 
?> 

任何人都会介意帮助完成这两项任务吗?任何投入将不胜感激。先谢谢你。

回答

0

你需要设置 “posts_per_page” 为-1一些其他的价值,它的文档说明以及

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

posts_per_page(INT) - 后每页显示的号码(可 自版本2.1起,替换showposts参数)。使用 'posts_per_page'=> - 1显示所有帖子('offset'参数为 ,被-1值忽略)。如果在使用此参数后关闭页面分页为 ,请设置'分页'参数。注意:如果查询在Feed中, wordpress将使用存储的'posts_per_rss' 选项覆盖此参数。要重新设置限制,请尝试使用'post_limits'过滤器或 过滤器'pre_option_posts_per_rss'并返回-1