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

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

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上