1

我正在为我的网站创建一个存档页面。我创建了一个带有存档模板的页面,并且正在使用 WP_Query 来获取与某些变量匹配的帖子。我使用以下代码:

$myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5)); ?>

<?php if($myposts->have_posts()){
        while ( $myposts->have_posts() ) {
        $myposts->the_post();

            include 'article-box.php';
        }
    } ?>

其中 'articles.php 生成帖子。然后是不起作用的部分。

<div id="pages-nav">
        <div class="alignleft"><?php previous_posts_link('&laquo; Previous Articles') ?></div>
        <div class="alignright"><?php next_posts_link('Next Articles &raquo;','') ?></div>
</div>

下一页/上一页的链接根本不显示。这与我使用带有存档模板的页面这一事实有关,还是我还缺少其他任何东西?

如果我输入
[http://localhost/wordpress/archives/page/2/?date=201307&category=5&writer=0&submit=search]
作为 URL,我会得到下一个帖子,因为它应该是“上一个”链接而不是“下一个”链接

编辑:

print_r($myposts) 返回:

WP_Query Object ( [query_vars] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 [error] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => carolynne [tag] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [include_children] => [field] => term_id [operator] => IN ) ) [relation] => AND ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => ) [post_count] => 5 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 22 [max_num_pages] => 5 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => 1 [is_year] => [is_month] => 1 [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash] => 5d9c9d82fe6cd2bec72ca33ab10482e7 [query_vars_changed] => [thumbnails_cached] => [query] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 ) [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND YEAR(wp_posts.post_date)=2013 AND MONTH(wp_posts.post_date)=07 AND ( wp_term_relationships.term_taxonomy_id IN (7,10,11,12,56) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5 [posts] => Array ( [0] => WP_Post Object ( [ID] => 43393 [post_author] => 1013 [post_date] => 2013-07-31 18:17:44 [post_date_gmt] => 2013-07-31 17:17:44 [post_content] => This film is a cross [...]
4

1 回答 1

1

发送$paged变量进行查询,如下所示:

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
 $myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5,'paged' => $paged));    

并将您的分页代码更改为以下内容:

<div id="pages-nav"> 
    <div class="alignleft">
       <?php previous_posts_link('&laquo; Previous Articles',$myposts->max_num_pages); ?>  
    </div>
    <div class="alignright">
       <?php next_posts_link('Next Articles &raquo;',$myposts->max_num_pages); ?>
    </div> 
</div>
于 2013-08-05T10:48:57.083 回答