如果需要调用一定时间段内的WordPress文章,可以通过下面的代码实现。
- <?php
- $cat = '2'; // 分类ID
- // 获取子分类,用于排除子分类文章
- $args = array( 'parent' => $cat );
- $categories = get_categories( $args );
-
- $excludecat = array();
- foreach ( $categories as $category ) {
- $excludecat[] = $category->cat_ID;
- }
-
- $args = array(
- 'cat' => $cat, // 分类ID
- 'posts_per_page' => '10', // 显示篇数
- 'ignore_sticky_posts' => true, // 排除置顶
- 'category__not_in' => $excludecat, // 排除子分类文章
- 'date_query' => array(
- array(
- // 开始年月日
- 'after' => array(
- 'year' => '2022',
- 'month' => '12',
- 'day' => '1',
- ),
- // 结束年月日
- 'before' => array(
- 'year' => '2023',
- 'month' => '12',
- 'day' => '31',
- ),
-
- 'inclusive' => true, // 包括当日
- ),
- ),
- );
-
- $query = new WP_Query( $args );
- ?>
-
- <ul>
- <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>
- <li>
- <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
- </li>
- <?php endwhile;?>
- <?php wp_reset_postdata(); ?>
- <?php else : ?>
- <li>
- 暂无文章
- </li>
- <?php endif;?>
- </ul>
复制代码
|