我正在努力弄清楚为什么我有这么困难的时间让WordPress只显示超过30天的帖子而且我真的可以使用第二组眼睛。我正在使用以下代码,但我的网站上没有显示任何内容。
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="meta">
<span class="date">
<strong><?php the_time('d'); ?></strong>
<strong><?php the_time('M'); ?></strong>
</span>
</div>
<div class="entry">
<?php if ( function_exists( 'get_the_image' ) ) {
get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '170', 'height' => '155' ) ); }
?>
<?php the_content('Read More'); ?>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php
include('includes/wp-pagenavi.php');
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
答案 0 :(得分:7)
<?php
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
这有效
答案 1 :(得分:1)
你做的方式看起来很好。尝试将posts_per_page => -1
作为参数。
以下适用于我:
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$args = array(
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );
while ($the_query->have_posts()) {
$the_query->the_post();
// do stuff
}
希望它有所帮助。