2013-06-27 75 views
3

我最近有很多幫助創建即將發生的事件列表(請參閱此處Showing upcoming events (including todays event)?),因此使用WP Pagenavi的分頁打破。分頁顯示所有其他頁面上的第1頁相同的帖子

目前,當您點擊第2頁時,它只顯示與第一頁相同的帖子。儘管該網址並真正改變頁/ 2頁/ 3等

我有這個在我functions.php文件:

function filter_where($where = '') { 
    $where .= " AND post_date >= '" . date("Y-m-d") . "'"; 
    return $where; 
} 

add_filter('posts_where', 'filter_where'); 

$query = new WP_Query(
    array(
     'post__not_in' => array(4269), 
     'paged' => get_query_var('paged'), 
     'post_type' => 'whatson', 
     'exclude' => '4269', 
     'post_status' => 'future,publish', 
     'posts_per_page' => 20, 
     'order' => 'ASC' 
    ) 
); 

remove_filter('posts_where', 'filter_where'); 

我環路則如下:

<?php while ($query->have_posts()) : $query->the_post(); ?> 
// content 
<?php endwhile; // end of the loop. ?> 
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $query)); } ?> 

回答

3

最後用解決了這個

<?php 
$wp_query = array(
     'post__not_in' => array(4269), 
     'paged' => get_query_var('paged'), 
     'post_type' => 'whatson', 
     'exclude' => '4269', 
     'posts_per_page' => 20, 
     'order' => 'ASC', 
     'orderby' => 'date', 
     'post_status' =>array('future','published')); 
query_posts($wp_query); 
?> 

<?php 
if ($wp_query->have_posts()) { 
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 
     Content 
    <?php endwhile; // end of the loop. 
} ?> 

<?php if (function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $wp_query)); } ?> 
+0

順便說一句。之所以不起作用,是因爲在靜態頁面上輸出帖子時,你必須使用'get_query_var('page')'而不是'get_query_var'''paged')' – n1te

+0

@ n1te不是,兩者之間的改變沒有任何區別。 – Rob

1

你想要它的特定職位或所有的人?如果你想一般分頁可以使無插件分頁鏈接使用這段代碼:

<?php 
global $wp_query; 
$big = 999999999; // need an unlikely integer 
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)), 
'format' => '?paged=%#%', 
'current' => max(1, get_query_var('paged')), 
'total' => $wp_query->max_num_pages 
)); 
?> 

只需將其添加到您的index.php或archives.php,看到奇蹟發生了:)

+0

我想爲所有的人。如果可能的話,我想盡力讓它與我所擁有的一起工作。 – Rob

1

我不知道你的代碼的其他部分發生了什麼,但有一件事要做,那就是在你的new WP_Query之前使用wp_reset_query()來確保查詢變量沒有被修改。

function my_filter_where($where = '') { 
    global $wp_query; 
    if (is_array($wp_query->query_vars['post_status'])) { 

     if (in_array('future',$wp_query->query_vars['post_status'])) { 
     // posts today into the future 
     $where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'"; 
     } 
    } 
    return $where; 
} 
add_filter('posts_where', 'my_filter_where'); 

而且:

+0

我在幾個不同的地方給了它一個去,但現在運氣好。 – Rob

相關問題