【发布时间】:2018-01-08 21:16:34
【问题描述】:
我目前正在使用 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() );
?>
有人介意帮忙完成这两项任务吗?任何投入将不胜感激。先感谢您。
【问题讨论】:
标签: php wordpress foreach offset categories