【问题标题】:Wordpress Pagination - max_num_pages = 0Wordpress 分页 - max_num_pages = 0
【发布时间】: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


【解决方案1】:

这里有几个问题。您无需调用全局$wp_query,因为您不会使用它。其次,array_merge 完全不合适。在这种情况下根本不应该使用它。你的代码应该是这样的

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $args = array( 
    'post_type' => 'attachment',
    'posts_per_page' => 10,
    'paged' => $paged                                       
     )
 );

$query = new WP_Query( $args );

有关WP_Query 的自定义查询的更多信息,请查看here

【讨论】:

  • 我根据您的更改更新了代码,但 max_num_pages 仍然返回 0
  • 今天下午回家后看看我能做什么。现在用我的手机发帖
  • 感谢您的帮助 :) 我真的不明白相同的数组参数如何用于 get_posts() 但不适用于 new WP_Query()
  • 这个问题可能是附件没有后父的原因吗? IE。我正在尝试获取当前类别的附件列表。当 URL 为 /category/member-resources/ 时,查询将相应设置 category
  • 如果我错了,请纠正我,您需要在类别页面上显示属于正在查看的特定类别的帖子的附件
猜你喜欢
  • 2015-03-07
  • 2020-01-30
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 2018-12-18
相关资源
最近更新 更多