WordPress开发笔记

WordPress文章页中上一篇、下一篇配置方法

基础配置方法

这样:

<?php
$prev_post = get_previous_post();
if ( ! empty( $prev_post ) ): ?>
	<a href="<?php echo get_permalink( $prev_post->ID ); ?>">
		<?php echo apply_filters( 'the_title', $prev_post->post_title ); ?>
	</a>
<?php endif; ?>

或者,


//或者添加分类法名称,并且要求应位于同一term分类术语中 ( $zzw_tax---分类法名称 )
//这样:
//$previous_post_ob = get_previous_post(true, '', $zzw_tax);
//$next_post_ob = get_next_post(true, '', $zzw_tax);

//或者最简单的:
$previous_post_ob = get_previous_post();
$next_post_ob = get_next_post();

//上架:
if ( $previous_post_ob ) {
  $previous_post = '<a href="' . get_permalink($previous_post_ob->ID) . '" class="d-block">' . get_the_title($previous_post_ob->ID) . '</a>';
}
if ( $next_post_ob ) {
  $next_post = '<a href="' . get_permalink($next_post_ob->ID) . '" class="d-block">' . get_the_title($next_post_ob->ID) . '</a>';
}

高级配置方法

背景

使用“分类+分类中文章列表”这样的列表循环样板时,这样的需求就比较显得体验更优。尤其是在顶级分类下存在多个子分类时。需要的列表是将该顶级分类下所有子分类及其中内容标题全部列出。

需求

当文章为其所属某个term分类中的第一篇时,上一篇的位置显示为该term分类的链接;

当文章为其所属某个term分类中的最后一篇时,下一篇的位置显示为该term分类的同级分类链接,并且是同一分类法、同父分类中第一个term_id大于该分类term_id的分类。如果该文章所属的这个term分类是顶级分类,那么下一篇的位置则留空。()

注意事项

注意term分类的排序,当文章所属分类存在顶级分类时,将获取该顶级分类下的所有子分类,这里获取的是子分类term_id组成的数组,并使用sort()函数进行了排序,以便精准满足需求2.

主题模板上配置

这里仅仅将$previous_post 与 $next_post 获取得到HTML。

//获取上一篇下一篇
//注意:承接了上文
$previous_post = '';
$next_post = '';
$zzw_posttype = get_post_type();
if ( $zzw_posttype ) {
  $zzw_tax = $zzw_posttype.'2nav';
  // 获取当前文章所属的 bjj2nav 分类术语
  $zzw_terms = wp_get_post_terms(get_the_ID(), $zzw_tax);// 获取文章在指定分类法下的术语对象的数组
  if ( !is_wp_error( $zzw_terms ) && !empty( $zzw_terms ) ) {
    $zzw_firstterm = reset($zzw_terms);//获取第一个元素值
    $previous_post_ob = get_previous_post(true, '', $zzw_tax);
    $next_post_ob = get_next_post(true, '', $zzw_tax);
    if ( $previous_post_ob ) {
      $previous_post = '<a href="' . get_permalink($previous_post_ob->ID) . '" >' . get_the_title($previous_post_ob->ID) . '</a>';
    }
    if ( $next_post_ob ) {
      $next_post = '<a href="' . get_permalink($next_post_ob->ID) . '">' . get_the_title($next_post_ob->ID) . '</a>';
    }

    if ( zzw_first_post_in_term($zzw_tax,$zzw_firstterm->term_id,get_the_ID()) ) {//该函数在functions.php中定义
      $previous_post = '<a href="' . get_term_link($zzw_firstterm) . '">' . $zzw_firstterm->name . '</a>';
    }
    if ( zzw_last_post_in_term($zzw_tax,$zzw_firstterm->term_id,get_the_ID()) ) {//该函数在functions.php中定义
      if ( $zzw_firstterm->parent === 0 ) {
        $next_post = '';
      } else {
        $zzw_terms = get_terms( array(
            'taxonomy'   => $zzw_tax,
            'parent'     => $zzw_firstterm->parent,
            'hide_empty' => false,
            'fields'     => 'ids',
        ) );
        sort($zzw_terms);
        $zzw_terms_end = end($zzw_terms);
        if ( $zzw_terms_end === $zzw_firstterm->term_id ) {
          $next_post = '';
        } else {
          $zzw_next_termid = zzw_FirstValue($zzw_terms, $zzw_firstterm->term_id);//该函数在functions.php中定义:获取数组中第一个大于某个指定值的值
          $zzw_next_termob = get_term($zzw_next_termid,$zzw_tax);
          $next_post = '<a href="' . get_term_link($zzw_next_termob) . '">' . $zzw_next_termob->name . '</a>';
        }
      } 
    }
  } else {
    // 获取上一篇和下一篇
    $previous_post_ob = get_previous_post();
    $next_post_ob = get_next_post();
    if ( $previous_post_ob ) {
      $previous_post = '<a href="' . get_permalink($previous_post_ob->ID) . '">' . get_the_title($previous_post_ob->ID) . '</a>';
    }
    if ( $next_post_ob ) {
      $next_post = '<a href="' . get_permalink($next_post_ob->ID) . '">' . get_the_title($next_post_ob->ID) . '</a>';
    }
    
  }
}

在你需要的位置打印

将他们放在你想要放的位置。

<?php  echo $previous_post; //打印上一篇 ?>
<?php  echo $next_post; //打印下一篇 ?>

如果有外围HTML,建议先判断一下是否有值,再配置渲染。

比如:

<?php 
  if ( $previous_post ) {
    ?>
      <div class="d-flex">
        <div class="*"></div>
        <?php 
        echo $previous_post;
        ?>
      </div>
    <?php 
  } 
?>

主题functions.php中配置

将以下函数放置到主题functions.php中去,从上面的代码的标注中你已经看到有3个函数是需要配置到主题functions.php中的。

//WordPress文章页面是否为其所属的某个term分类中的第一篇文章的函数
if ( ! function_exists( 'zzw_is_first_post_in_term' ) ) :
  function zzw_is_first_post_in_term($tax_name,$term_id,$post_id) {
      // 验证当前是否为单篇文章页面且属于分类"tt2"
      if (!is_single() || !has_term($term_id, $tax_name, $post_id) ) {
          return false;
      }

      // 查询分类"tt2"下按发布时间降序排列的最新文章
      $args = array(
          'tax_query' => array(
              array(
                  'taxonomy' => $tax_name, // 若为自定义分类法需替换名称
                  'field'    => 'term_id',
                  'terms'    => $term_id,
              )
          ),
          'orderby'        => 'date',
          'order'          => 'ASC',//或者: ASC 升序、DESC 降序
          'posts_per_page' => 1,
          'post_status'    => 'publish'
      );

      $query = new WP_Query($args);
      $is_first = false;

      // 比较当前文章与查询结果
      if ($query->have_posts()) {
        while ($query->have_posts()) {
          $query->the_post();
            if ( get_the_ID() === $post_id ) {
              $is_first = true;
            }
        }
      }

      // 重置查询防止影响后续循环
      wp_reset_postdata();
      
      return $is_first;
  }
endif;


//WordPress文章页面是否为其所属的某个term分类中的最后一篇文章的函数
if ( ! function_exists( 'zzw_is_last_post_in_term' ) ) :
    function zzw_is_last_post_in_term($tax_name,$term_id,$post_id) {
        // 验证当前是否为单篇文章页面且属于分类"tt2"
        if (!is_single() || !has_term($term_id, $tax_name, $post_id) ) {
            return false;
        }

        // 查询分类"tt2"下按发布时间降序排列的最新文章
        $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => $tax_name, // 若为自定义分类法需替换名称
                    'field'    => 'term_id',
                    'terms'    => $term_id,
                )
            ),
            'orderby'        => 'date',
            'order'          => 'DESC',//或者: ASC 升序、DESC 降序
            'posts_per_page' => 1,
            'post_status'    => 'publish'
        );

        $query = new WP_Query($args);
        $is_last = false;

        // 比较当前文章与查询结果
        if ($query->have_posts()) {
        while ($query->have_posts()) {
          $query->the_post();
            if ( get_the_ID() === $post_id ) {
              $is_last = true;
            }
        }
      }

        // 重置查询防止影响后续循环
        wp_reset_postdata();
        
        return $is_last;
    }
endif;

//判断函数:获取数组中第一个大于某个指定值的值
if ( ! function_exists( 'zzw_FirstValue' ) ) :
  function zzw_FirstValue($arr, $tb) {
    foreach ($arr as $value) {
        if ($value > $tb) {
            return $value; // 找到即返回
        }
    }
    return null; // 未找到时返回 null
  }
endif;