![](/img/trans.png)
[英]PHP Shortcode - How to accept category argument for WP blog posts
[英]How to show the week before posts from "You may have missed" post section in WP blog with PHP
我正在寫我的 WordPress 博客,在頁腳區域我有一個包含 4 個帖子的 slider。 那個 slider 被稱為“你可能錯過了”,目前它正在顯示最新的帖子。 使用 'rand' arg 我設法對其顯示隨機帖子進行排序,但我擔心它可能會向后 go 幾個月。 我想排除當前一周的帖子,只顯示帖子前一周,這樣我就可以在我的博客中有一個有效的“你可能錯過了”。 例如,如果當前周是 34,則僅顯示第 33 周的帖子,如果是第 45 周,則僅顯示第 44 周的帖子等。我想通過 arrays 解決它。
這是我目前擁有的
$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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.