分页在自定义页面中不起作用,仅在POST页面中起作用

时间:2020-04-04 作者:bobby kumar
global $wp_query;
$result = $wp_query;
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$original_query = $wp_query;
$wp_query = null;
$args=array(
    \'post_type\'=> \'post\',
    \'orderby\'    => \'ID\',
    \'post_status\' => \'publish\',
    \'order\'    => \'DESC\',         
    \'posts_per_page\'=>get_option( \'posts_per_page\' ),
    \'paged\'=>$paged
);
$wp_query = new WP_Query( $args );
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
    <?php while ( $result->have_posts() ) : $result->the_post(); 
          get_template_part( \'template-parts/content\', get_post_type() );
     endwhile; 
     echo "<div class=\'pagenum_link\'>";
         $total_pages = $result->max_num_pages;
         if ($total_pages > 1){
             $current_page = max(1, get_query_var(\'paged\'));
             echo paginate_links(array(
                 \'base\' => get_pagenum_link(1) . \'%_%\',
                 \'format\' => \'/page/%#%\',
                 \'current\' => $current_page,
                 \'total\' => $total_pages,
                 \'prev_text\'    => __(\'«\'),
                 \'next_text\'    => __(\'»\'),
             ));
         }
     echo "</div>";
else :
     get_template_part( \'template-parts/content\', \'none\' );
endif; 
wp_reset_postdata(); ?>
1 个回复
SO网友:TomC

从中删除以下行$args 阵列:

\'post_type\'=> \'post\',
或将其更改为:

\'post_type\'=> \'any\',
这只是在帖子上运行查询,不会在页面上运行。下面将对此进行更详细的解释:https://wordpress.org/support/article/post-types/

相关推荐

突出显示第一页Pagination_Links

我需要WordPress的帮助。我不是WordPress开发人员,但出于某种原因,我必须在这里更改一些代码。现在说到点子上。我有一个页面,显示一些分页内容。Im使用paginate_links() 生成分页。当我在第一页时,页码不会突出显示它。但从第二页开始,它会突出显示页码。但我也要强调第一点。以下是我使用的代码:<?php $args = array( \'base\' => \'%_%\', \'format\' => \'#page%#%\