2012-01-25 31 views
1

我正在努力弄清楚爲什麼我有這麼困難的時間讓WordPress只顯示比30天更新的帖子,我真的可以使用第二套眼睛。我正在使用下面的代碼,但沒有顯示在我的網站上。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; ?> 

回答

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 
} 

希望它能幫助。

+0

感謝您的幫助,但是,遺憾的是,沒有工作。我絕對難過,因爲我最近幾天有大約4-5個職位,他們沒有出現。應該很容易,但由於某種原因,我有一段時間。 – user965879

+0

它可以是你的代碼周圍的東西。嘗試做一個最小的工作示例。停用所有插件,將所有內容放入一個函數中,最後輸出結果和'exit()',並用'add_action('wp','your_func');'掛鉤它。可能真的很愚蠢。 = d – vmassuchetto

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); 
?> 

這工作