【发布时间】:2014-09-04 17:22:35
【问题描述】:
Wordpress 忍者...我需要一些关于分页的帮助。
我有以下代码:
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge( $wp_query->query_vars,
array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
$query->max_num_pages 怎么会返回 0 ?
有没有办法用 $query->max_num_pages 更改原始存档查询?
[编辑]
除了上面的代码,我还有以下
$output = '<div class="download-attachments left_content_container"><ul>';
//this does not retrieve anything
$query = new WP_Query( $args );
// this does retrieve posts
$attachments = get_posts( $args );
// this block is only used for testing purposes..
// ie. it is not being executed because the query does not return anything, Whilst get_posts() works
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo the_ID();
}
}
foreach ($attachments as $attachment) {
setup_postdata($attachment);
//get attachments metadata etc.
$url = wp_get_attachment_url( $attachment->ID );
$title = get_the_title( $attachment );
$caption = $attachment->post_excerpt;
$size = get_readable_size( filesize(get_attached_file( $attachment->ID )) );
$icon = get_icon_for_attachment( $attachment->ID );
$mime = get_post_mime_type( $attachment->ID );
$date_added = $attachment->post_date;
$filename = ( !$caption == '' ) ? $caption : $title ;
$description = ($attachment->post_content != '') ? '<span class="attachment-author"><span class="attachemnt-label">Added by: ' . $attachment->post_content . '</span></span>' : '';
$output .= '<li class="'.$mime.'">
<img class="attachment-icon" src="'.$icon.'" alt="pdf">
<a href="'. home_url().'/wp-content/plugins/download-attachments/includes/download.php?id='.$attachment->ID.'" class="attachment-link" title="'.$filename.'">'. $filename .'</a> '.$description.'
<span class="attachment-date"><span class="attachment-label">Date added: </span>'.$date_added.'</span>
<span class="attachment-size"><span class="attachment-label">Attachment size: </span>'.$size.'</span></li>' ;
}
$output .= '</ul></div>';
echo $output;
echo '<div class="paging pull-left">';
$paged = $wp_query->get( 'paged' );
if ( ! $paged) {/* do nothing */ ;}
else { bootstrap_pagination(); var_dump($wp_query->max_num_pages); // returns 0 }
echo '</div>';
【问题讨论】:
-
你在哪里尝试这样做。此外,您的代码看起来很奇怪
-
这有什么奇怪的?我正在尝试在 archive.php 中执行此操作。难道是一旦你改变了主查询,你需要重置它才能使用
max_num_pages? -
不,问题是,您的代码完全错误。会在睡觉前快速发布代码:-)
-
先生,请启发我,因为我对 wordpress 世界完全陌生 :)
标签: php wordpress pagination