使用分页时,模板标记和样式将消失


Template markup and styling disappears when using pagination

我正试图在自定义wordpress主题中创建一个分页,但我无法解决问题。基本上,我得到的是一个名为"Portfolio page"的自定义页面模板,它位于page-portfolio.php中,我每页最多显示5篇文章。

当我点击转到下一个包含旧帖子或新帖子的页面时,我会看到portfolio/page/page-number-here/的url,而原始url是portfolio/,它有所有的标记和内容。当url不再位于portfolio/时,这将导致所有标记和样式消失。我该如何将较旧或较新的帖子加载到同一模板中?

我一直在搜索codex和支持以寻找答案,但我甚至找不到其他有这个问题的人。我确信我错过了导致这种情况发生的重要事情。

这是我的page-portfolio.php:

<?php /* Template Name: Portfolio Page */ ?>
<?php get_header(); ?>
<?php
  $num_posts = ( is_front_page() ) ? 1 : 5;
  $args = array(
    'post_type' => 'portfolio',
    'posts_per_page' => $num_posts
  );
  $query = new WP_Query( $args );
?>
<section id="portfolio-wrapper">
  <ul id="portfolio-list" class="list-reset">
  <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <li class="clear-fix portfolio-piece">
      <h1 class="portfolio-piece-title"><?php the_title(); ?></h1>
      <div class="portfolio-piece-gallery"><?php the_field('images'); ?></div>
      <div class="portfolio-piece-content"><?php the_content(); ?></div>
    </li>
  <?php endwhile; ?>
  </ul>
  <div class="pagination-wrapper">
    <?php 
      if ( function_exists( 'pagination' ) ) {
        pagination( $query->max_num_pages );
      } 
    ?>
    <?php endif; wp_reset_postdata(); ?>
  </div>
</section>
<?php get_footer(); ?>

我正在使用一个分页函数,它看起来像这样:

function pagination( $pages = '', $range = 4 ) {  
   $showitems = ( $range * 2 ) + 1;  
   global $paged;
   if ( empty( $paged ) ) {
     $paged = 1;
   }
   if ( $pages == '' ) {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if ( !$pages ) {
       $pages = 1;
     }
   }   
   if ( 1 != $pages ) {
     echo "<div class='"pagination'"><span>Page ".$paged." of ".$pages."</span>";
     if ( $paged > 2 && $paged > $range+1 && $showitems < $pages) { 
       echo "<a href='".get_pagenum_link( 1 )."'>&laquo; First</a>";
     }
     if ( $paged > 1 && $showitems < $pages ) {
       echo "<a href='".get_pagenum_link( $paged - 1 )."'>&lsaquo; Previous</a>";
     }
     for ( $i = 1; $i <= $pages; $i++ ) {
       if ( 1 != $pages &&( !( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) {
         echo ( $paged == $i ) ? "<span class='"current'">".$i."</span>":"<a href='".get_pagenum_link( $i )."' class='"inactive'">".$i."</a>";
       }
     }
     if ( $paged < $pages && $showitems < $pages) {
       echo "<a href='"".get_pagenum_link( $paged + 1 )."'">Next &rsaquo;</a>";
     }
     if ($paged < $pages - 1 &&  $paged + $range - 1 < $pages && $showitems < $pages) {
       echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
     }
     echo "</div>'n";
   }
}

有几个问题可能会导致您遇到的问题。首先,在wordpress中,让一个页面和一个帖子类型共享同一个slug——在你的案例中是"投资组合",这是一个有据可查的问题。这里有一个简单的解决方法,因为你使用的是一个自定义模板,而不是这个帖子类型的存档模板,那就是在你注册公文包帖子类型时禁用它的存档-

'has_archive' => false

https://codex.wordpress.org/Function_Reference/register_post_type

在更改后,我还会通过重新保存永久链接设置来刷新您的重写规则,以确保这不会引起任何问题。

此外,如果查询中不包含paged变量,分页将无法正常工作。首先声明它,然后将其包含在您的查询中:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type'      => 'portfolio',
    'posts_per_page' => $num_posts,
    'paged'          => $paged,
);
$query = new WP_Query( $args );

https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters