1
我想在我的網站上創建一個熱門的帖子部分,以顯示過去一週前5個最熱門的帖子。熱門帖子wp_query和date_query wordpress
功能的熱門帖子
function shapeSpace_popular_posts($post_id) {
$count_key = 'popular_posts';
$count = get_post_meta($post_id, $count_key, true);
if ($count < 1) {
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
function shapeSpace_track_posts($post_id) {
if (!is_single()) return;
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
shapeSpace_popular_posts($post_id);
}
add_action('wp_head', 'shapeSpace_track_posts');
循環流行的帖子
<?php $popular = new WP_Query(array(
'posts_per_page'=>5,
'date_query' => array(
//set date ranges with strings!
'after' => '1 week ago',
'before' => 'today',
//allow exact matches to be returned
'inclusive' => true,
),
'meta_key'=>'popular_posts',
'orderby'=>'meta_value_num',
'order'=>'DESC'));
while ($popular->have_posts()) : $popular->the_post(); ?>
<h5 class="section_category">
<?php the_category(', ') ?>
</h5>
<div class="trending_title">
<?php the_title(sprintf('<h4><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h4>'); ?>
</div>
<h5 class="section_author">
By <?php coauthors_posts_links(); ?>
</h5>
<div class="border-bottom"></div>
<?php endwhile; wp_reset_postdata(); ?>
如果你能幫助反正我將不勝感激。
謝謝
它似乎有所作爲,當我這個替換。任何建議爲什麼? – Ian
一切工作很好,直到date_query那麼它似乎是沒有反應,我不確定爲什麼 – Ian
你可以嘗試'列'=>'post_date','後'=>' - 7天',而不是'後'=>' 1周前', –