如何使用PHP显示WP博客中"You may have missed"帖子部分的帖子前一周



我正在写我的WordPress博客,在页脚区域我有一个由4篇文章组成的滑块。该滑块被称为"滑块";你可能错过了";目前它正在显示最新的帖子。使用"rand"arg,我设法对它显示的帖子进行了排序,但我担心它可能会倒退几个月。我想排除本周的帖子,只显示帖子前一周,这样我就可以有一个有效的"你";你可能错过了";在我的博客中。例如,如果当前周是34周,则只显示第33周的帖子,如果是第45周,则仅显示第44周的帖子等等。我想通过数组来解决它。

这就是我目前拥有的

$footer_post_type = array(
'posts_per_page' => 4,
'post__not_in' => get_option('sticky_posts'),
'year' => date( 'Y' ),
'week' => strftime( '%U' ),
'orderby' => 'rand',
'post_type' => array(
'post'
),

应该非常简单。只需要从你当前的一周中减去1。

您可以将此代码缩短一点,但包括用于演示目的的所有内容:

$week = date( 'W' ); // Get current week of the year
$year = date( 'Y' ); // Get current year
$last_week = $week - 1; // Subtract 1 to get previous week number

// Create your query args
$args = array(
'posts_per_page' => 4,
'post__not_in' => get_option('sticky_posts'),
'date_query' => array(
array(
'year' => $year, // Year here
'week' => $last_week, // Week here
),
),
'orderby' => 'rand',
'post_type' => 'post'

);
$query = new WP_Query( $args );

最新更新