programing

워드프레스페이지네이션wp_query->max_num_pages return 0

instargram 2023. 10. 29. 19:01
반응형

워드프레스페이지네이션wp_query->max_num_pages return 0

워드프레스 커스텀 페이지의 페이지 페이지에 문제가 있습니다.템플릿 눈부신 기능을 사용합니다. https://github.com/puikinsh/Dazzling/blob/master/inc/template-tags.php 그러나 항상 $GLOBALS['wp_query']->max_num_pages는 0(null)입니다.변경하려고 여러 번 시도합니다. 지금은 이렇게 합니다.

 $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'post',
    'category__in' => '4',
    'posts_per_page' => 3,
    'max_num_pages' => 5,
    'paged' => $paged
  );
  // create a new instance of WP_Query
  $the_query = new WP_Query( $query_args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
  <article>
    <h1><?php echo the_title(); ?></h1>
    <div class="excerpt">
      <?php the_excerpt(); ?>
    </div>
  </article>
<?php endwhile; ?>

<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
<?php dazzling_paging_nav()); ?>

<?php } ?>

<?php else: ?>
  <article>
    <h1>Sorry...</h1>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  </article>
<?php endif; ?>

내가 뭘 잘못한 거지?감 잡히는 게 없어요?

누군가에게 도움이 될지는 잘 모르겠지만 만일의 경우를 대비해서요.저는 같은 문제($wp_query->max_pages_number returning 0)를 가지고 있었는데 매우 어리석은 일 때문에 막혔습니다.

저는 이 코드가 제 테마의 다른 곳에 있다는 것을 깨달았습니다.

function no_rows_found_function($query)
{ 
  $query->set('no_found_rows', true); 
}

add_action('pre_get_posts', 'no_rows_found_function');

WP_Query의 성능을 향상시키는 데 유용했지만 no_found_rows는 페이지를 비활성화합니다.플러그인이나 테마의 어딘가에 있지 않은지 확인합니다. :)

max_num_pages가 페이지 템플릿에서는 작동하지 않지만 아카이브 템플릿에서는 작동하는 동일한 문제가 있었습니다.저에게 해결책은 약간의 PHP였습니다.

  1. 게시된 모든 게시물 수
  2. 옵션에 설정된 페이지당 게시물 가져오기
  3. 최대한 반올림합니다.

        $published_posts = wp_count_posts()->publish;
        $posts_per_page = get_option('posts_per_page');
        $page_number_max = ceil($published_posts / $posts_per_page);
    
global $wp_query;
global $page, $numpages, $multipage, $more;

if( is_singular() ) {
    $page_key = 'page';
    $paged = $page;
    $max = $numpages;
} else {
    $page_key = 'paged';
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max = $wp_query->max_num_pages;
}

언급URL : https://stackoverflow.com/questions/27849312/wordpress-pagination-wp-query-max-num-pages-return-0

반응형