在另一个帖子类型的单页中自定义帖子类型的分页-Wordpress

时间:2019-08-18 20:29:51

标签: php wordpress pagination custom-post-type

我有两种自定义帖子类型,CategoriesVideos。每个类别都有多个视频。我有一个用于categories的页面,该类别的单个页面显示了在这些类别下添加的videos

现在,我希望将分页添加到单个类别页面中显示的视频中。

以下是single-categories.php

<?php
get_header();

//Page Title Bar
echo page_title_bar();
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php
    $category_id = get_the_ID();
    $comingsoon = get_field('coming_soon');
    $description = get_field('category_description');
?>

<!-- Page Container -->
<div class="container page-container">
  <?php  $args = array(
      'numberposts' => -1,
      'post_type'   => 'Videos',
      'order'       => 'ASC',
      'post_status'=>'publish'
  );
  $the_query = new WP_Query($args);
  if ($the_query->have_posts()):
      while ($the_query->have_posts()) : $the_query->the_post(); ?>
        <!-- HTML of Videos -->
      <?php endwhile; ?>

      <!-- Pagination -->
      <div class="pagination-wrapper">
            <?php pagination(); ?>
      </div>
      <!-- End of Pagination -->

  <?php endif; ?>
</div>
<!-- End Page Container -->

<?php endwhile; endif;?>

<?php get_footer(); ?>

pagination()功能已添加到function.php

function pagination($pages = '', $range = 4, $paged = null) {
    if ($paged == null) {
        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 "<ul class='pagination'><li><a>Page " . $paged . " of " . $pages . "</a></li>";
        echo '<li><a href="' . get_previous_posts_page_link() . '">«</a></li>'; 
        for ($i = 1; $i <= $pages; $i++) {
            if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
                echo ($paged == $i) ? "<li class=\"active\"><a>" . $i . "</a></li>" : "<li><a href='" . get_pagenum_link($i) . "' class=\"inactive\">" . $i . "</a></li>";
            }
        }
        echo '<li><a href="' . get_next_posts_page_link($pages) . '">»</a></li></ul>';

    }
}

以上代码的作用是过滤视频数量(如果将读数设置为10,则仅显示前10个视频),但不显示下一页,上一页按钮。同时,在博客页面上的分页效果很好。

0 个答案:

没有答案