dtsps2098 2017-08-01 20:46
浏览 82

WP偏移不适用于Foreach循环

I'm currently working a wordpress loop to retrieve blog posts, their titles, featured image, date and category. With that said, I'm attempting to offset the loop begin on the 5th descending post because the previous 4 are referenced earlier on the page.

I have successfully offset the posts but it seems that I can't grab the category.

<?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>

I tried a slightly different approach and was able to get each posts category using a foreach loop, followed by a while and if loop. While I successfully got each posts category, the offset wasn't cooperating. Perhaps I'm overthinking it. Here's my other attempt at this.

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

Anyone mind lending a hand to help accomplish both tasks? Any input would be greatly appreciated. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • doufan3408 2017-08-01 20:50
    关注

    you need to set "posts_per_page" to some other value than -1, it's explained well in documentation

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

    posts_per_page (int) - number of post to show per page (available since Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1

    评论

报告相同问题?