一个 WordPress 循环,仅显示您的 4 个类别中的最新 post
A WordPress loop that displays the latest post from just 4 of your categories
我是 WP 初学者,我编写了一个循环来显示我的每个类别中的最新 post,但我需要改进循环以便它只显示最新的 post 来自 4 个不同的类别。谁能帮我限制我循环浏览的类别数量?将不胜感激。
<div class="latest-updates-container container">
<div class="row">
<div class="col-lg-2">
<div class="latest-update-text">
LATEST UPDATES
</div>
</div>
</div>
<div class="latest-updates-outter-wrapper"></div>
<!--/.container-->
<div class="">
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => -2,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
if($i % 5 == 0) { ?>
<div class="row latest-post">
<?php
}
?>
<div class="col-lg-3">
<div class="my-inner">
<h5 id="post-<?php the_ID(); ?>" class="blog-heading">
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
</h5>
<div class="time-read-now"><?php echo reading_time(); ?> ·
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to">read now</a></h4>
</div>
</div>
</div>
<?php $i++;
if($i != 0 && $i % 5 == 0) { ?>
</div>
<!--/.row-->
<div class="clearfix"></div>
<?php
} ?>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div>
</div>
</div>
</div>
Its displaying the latest post from all categories, I need to just display 4 categories.
cat
参数允许您 filter posts by category ID。您的代码几乎是正确的,您现在需要做的就是将类别 ID 传递给 WP_Query
class:
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => $category->term_id, // Get posts from this category ID
'category__not_in' => array(4, 2), // Exclude posts from categories 4 and 2
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
// Rest of your code here...
我是 WP 初学者,我编写了一个循环来显示我的每个类别中的最新 post,但我需要改进循环以便它只显示最新的 post 来自 4 个不同的类别。谁能帮我限制我循环浏览的类别数量?将不胜感激。
<div class="latest-updates-container container">
<div class="row">
<div class="col-lg-2">
<div class="latest-update-text">
LATEST UPDATES
</div>
</div>
</div>
<div class="latest-updates-outter-wrapper"></div>
<!--/.container-->
<div class="">
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => -2,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
if($i % 5 == 0) { ?>
<div class="row latest-post">
<?php
}
?>
<div class="col-lg-3">
<div class="my-inner">
<h5 id="post-<?php the_ID(); ?>" class="blog-heading">
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
</h5>
<div class="time-read-now"><?php echo reading_time(); ?> ·
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to">read now</a></h4>
</div>
</div>
</div>
<?php $i++;
if($i != 0 && $i % 5 == 0) { ?>
</div>
<!--/.row-->
<div class="clearfix"></div>
<?php
} ?>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div>
</div>
</div>
</div>
Its displaying the latest post from all categories, I need to just display 4 categories.
cat
参数允许您 filter posts by category ID。您的代码几乎是正确的,您现在需要做的就是将类别 ID 传递给 WP_Query
class:
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => $category->term_id, // Get posts from this category ID
'category__not_in' => array(4, 2), // Exclude posts from categories 4 and 2
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
// Rest of your code here...