我不太明白为什么这不管用。我试图使用以下内容在首页上仅显示当年的帖子:
<?php query_posts( "&year=$current_year&order=DESC"); ?>
但它仍然显示2012年的帖子(它们实际上不是2012年发布的,但我将发布日期设置为其中一篇帖子,显示日期为去年2月)根据文件,我应该这样做。有人能解释一下吗?
谢谢
我不太明白为什么这不管用。我试图使用以下内容在首页上仅显示当年的帖子:
<?php query_posts( "&year=$current_year&order=DESC"); ?>
但它仍然显示2012年的帖子(它们实际上不是2012年发布的,但我将发布日期设置为其中一篇帖子,显示日期为去年2月)根据文件,我应该这样做。有人能解释一下吗?
谢谢
您只需获取当前日期并将其添加到wp\\U query中的data\\U query中,如下所示:
<?php
$getdate = getdate();
$args = array(
\'date_query\' => array(
array(
\'year\' => $getdate["year"]
),
),
);
$query = new WP_Query( $args );
?>
然后使用循环:<?php
if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post();
the_title();
the_content();
endwhile; endif;
?>
Don\'t use query_posts
to modify your queries. 这是对资源的浪费,而且会产生不可预测的结果,因为它会覆盖在加载模板之前发生的原始查询。
使用pre_get_posts
在您的主题中functions.php
而是在将查询发送到数据库之前和加载模板之前更改查询。
function home_page_current_year( $query ){
if( $query->is_home() && $query->is_main_query() )
$query->set( \'year\', date(\'Y\') );
}
add_action( \'pre_get_posts\', \'home_page_current_year\' );
我正在用以下代码显示相关帖子:<div class=\"my-related-posts\"><h4>You may also be interested in...</h4><ul class=\"relatedposts\"> <?php $posts = get_posts(\'numberposts=4&orderby=rand&category=\'. $category->term_id