我试图让寻呼在我未来的post循环中工作,但没有用。尽管数据库中有几个有效的帖子,但我没有得到用于分页的链接,而我希望它们位于底部。
<?php
$args = array(
\'post_type\' => \'program\',
\'paged\' => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1,
\'posts_per_page\' => 1,
\'post_status\' => "future",
\'order_by\' => \'date\',
\'order\' => \'ASC\'
);
$program = new WP_Query;
$program->query( $args );
if($program->have_posts()): while($program->have_posts()): $program->the_post(); ?>
//The content loop
<?php endwhile; ?>
<div><?php previous_posts_link(\'« Previous\'); ?></div>
<div><?php next_posts_link(\'More »\'); ?></div>
<?php endif; ?>
<?php wp_reset_query(); ?>
<小时>
Update 更多信息我已经确定$program->max\\u num\\u pages始终显示正确的页数,因此使用offset
的参数WP_Query
, 我已经能够自己翻页了。但这似乎是自找麻烦!!
<小时>Update 更多信息
我已经采取了相当激烈的步骤来扩展WP\\u查询对象。这确实有些过头了,但我必须做到这一点:
class MF_Query extends WP_Query{
function __construct(array $args){
if(!array_key_exists(\'posts_per_page\',$args)) $args[\'posts_per_page\'] = 10;
$args[\'offset\'] = (isset($_GET[\'page\'])?($_GET[\'page\']-1)*$args[\'posts_per_page\']:0);
parent::query($args);
}
function mf_next_page($link_text = "Next"){
$curPage = intval((isset($_GET[\'page\'])?$_GET[\'page\']:1));//Use 1 if $_GET[\'page\'] not set
$link = "<a href=\'".remove_post_vars(curPageURL());
if($curPage<$this->max_num_pages){
return $link.constructQuery($this->merge(array("page"=>$curPage+1),$_GET))."\'>".$link_text."</a>";
} else {
return false;
}
}
function mf_prev_page($link_text = "Prev"){
$curPage = (isset($_GET[\'page\'])?$_GET[\'page\']:1);//Use 1 if $_GET[\'page\'] not set
$link = "<a href=\'".remove_post_vars(curPageURL());
if($curPage>1){
return $link.$this->constructQuery($this->merge(array("page"=>$curPage-1),$_GET))."\'>$link_text</a>";
} else {
return false;
}
}
private function constructQuery(array $query){
$url_ext = "?";
foreach($query as $k => $v){
$url_ext .=$k."=".$v."&";
}
$url_ext = substr($url_ext, 0, -5);//chop last ampersand off
return $url_ext;
}
private function merge($get, $put){
//Get values from one array, and put them in another (overriding existing values if appropriate)
foreach ($get as $k => $v){
$put[$k]=$v;
}
return $put;
}
}