/ Wordpress

Wordpress 调用指定分类目录下的文章,并支持分页

在本教程中,我将向您展示如何使用分页创建自定义WordPress循环。我们将使用 WP_Query 类来实例化一个新的查询,并用分页显示帖子。

1.添加分页导航

functions.php 添加:

<code class="language-php">/**
 * 添加分页导航
 */
function site_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }


  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('«上一页'),
    'next_text'       => __('下一页»'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>当前所在:第<b>" . $paged ."</b>页". " 共" . $numpages . "页</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

}

2.页面调用

<code class="language-php"><?php
/**
 * Template Name: 自定义页面
 * 自定义页面模板文件
 */
?>

<?php get_header(); ?>

<?php

  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $custom_args = array(
      'post_type' => 'post',//文章类型
      'cat'=>3,  //分类的ID
      'posts_per_page' => 10,//每页显示几篇文章
      'paged' => $paged //页码
    );

  $custom_query = new WP_Query( $custom_args ); ?>

  <?php if ( $custom_query->have_posts() ) : ?>

    <!-- 循环 -->
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <article <?php post_class('item');?>>
        <h6><a href="<?php the_permalink();?>"><?php the_title();?></a></h6>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- 循环结束 -->

    <!-- 分页 -->
    <?php
      if (function_exists(site_pagination)) {
        site_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p>抱歉,未发现文章</p>
  <?php endif; ?>

<?php get_footer(); ?>

3.静态首页调用

这里静态页面指的是自定义里的静态首页:

<code class="language-php"><?php
/**
 * Template Name: 自定义页面-静态首页
 * 自定义页面模板文件
 */
?>

<?php get_header(); ?>


<?php

  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

  $custom_args = array(
      'post_type' => 'post',//文章类型
      'cat'=>3,  //分类的ID
      'posts_per_page' => 4,//每页显示几篇文章
      'paged' => $paged, //页码
      'page' => $paged
    );

  $custom_query = new WP_Query( $custom_args ); ?>

  <?php if ( $custom_query->have_posts() ) : ?>

    <!-- 循环 -->
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <article <?php post_class('item');?>>
        <h6><a href="<?php the_permalink();?>"><?php the_title();?></a></h6>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- 循环结束 -->

    <!-- 分页 -->
    <?php
      if (function_exists(site_pagination)) {
        site_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p>抱歉,未发现文章</p>
  <?php endif; ?>

<?php get_footer(); ?>

4.可能会遇到的问题

Nginx伪静态有可能会发生新建的页面结尾处没有斜线/,导致无法翻页,需要在functions.php 添加:

<code class="language-php">//给分类目录路径后面添加斜杠
function nice_trailingslashit($string, $type_of_url) {
    if ( $type_of_url != 'single' )
        $string = trailingslashit($string);
        return $string;
    }
add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);

Wordpress 调用指定分类目录下的文章,并支持分页
Share this