WordPress函数get_boundary_post()获取边界文章

WordPress函数get_boundary_post()获取边界文章

WordPress函数get_boundary_post()获取边界文章

  在 WordPress CMS内容管理系统中,get_boundary_post()是一个内置函数,需要根据 WordPress 插件或主题中的发布日期获取最后一篇或第一篇文章,您可以使用 get_boundary_post ()函数。在本文中,我们将向您展示如何使用get_boundary_post()函数。

  推荐:什么是Trackback?WordPress怎么禁用Trackback

get_boundary_post()函数基本语法

描述

  检索与当前帖子相邻的上一篇帖子

用法

<?php get_boundary_post($in_same_term,$excluded_terms,$start,$taxonomy); ?>

参数

  $in_same_term

  ( bool ) ( 可选) 它将确定帖子是否应属于同一类别。默认值:false

  $start

 (bool)(可选)将允许您定义它的开始位置。例如,如果您获得第一篇文章,这将被设置为true。如果获取最后一篇文章,您可以将其设置为false

  $taxonomy

  (string)(可选)分类法名称,如果 $in_same_term 为真。默认值:“category”

返回值

  • 如果成功,则发布对象。
  • 如果未设置全局$post,则为空。
  • 如果不存在相应的帖子,则为空字符串。

  推荐:如何修复WordPress后台响应速度慢

get_boundary_post()函数

  WordPress函数get_boundary_post()获取边界文章(源文件可参考这里

function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
	$post = get_post();

	if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) {
		return null;
	}

	$query_args = array(
		'posts_per_page'         => 1,
		'order'                  => $start ? 'ASC' : 'DESC',
		'update_post_term_cache' => false,
		'update_post_meta_cache' => false,
	);

	$term_array = array();

	if ( ! is_array( $excluded_terms ) ) {
		if ( ! empty( $excluded_terms ) ) {
			$excluded_terms = explode( ',', $excluded_terms );
		} else {
			$excluded_terms = array();
		}
	}

	if ( $in_same_term || ! empty( $excluded_terms ) ) {
		if ( $in_same_term ) {
			$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
		}

		if ( ! empty( $excluded_terms ) ) {
			$excluded_terms = array_map( 'intval', $excluded_terms );
			$excluded_terms = array_diff( $excluded_terms, $term_array );

			$inverse_terms = array();
			foreach ( $excluded_terms as $excluded_term ) {
				$inverse_terms[] = $excluded_term * -1;
			}
			$excluded_terms = $inverse_terms;
		}

		$query_args['tax_query'] = array(
			array(
				'taxonomy' => $taxonomy,
				'terms'    => array_merge( $term_array, $excluded_terms ),
			),
		);
	}

	return get_posts( $query_args );
}

  推荐:WordPress函数get_adjacent_post()获取相邻文章

如何使用get_previous_post()

获得同一类别的第一篇文章此示例检索与当前帖子属于同一类别的第一篇帖子。

$first_post = get_boundary_post(true, '', true);
if (!empty($first_post)) {
    echo '<a href="' . get_permalink($first_post->ID) . '">' . $first_post->post_title . '</a>';
}

获取同一类别中的最后一篇文章,此示例检索与当前帖子属于同一类别的最后一篇帖子

$last_post = get_boundary_post(true, '', false);
if (!empty($last_post)) {
    echo '<a href="' . get_permalink($last_post->ID) . '">' . $last_post->post_title . '</a>';
}

获取排除特定类别的第一篇文章,此示例检索第一篇文章,排除来自特定类别(例如,类别 ID 3)的文章。

$first_post_excluded = get_boundary_post(false, '3', true);
if (!empty($first_post_excluded)) {
    echo '<a href="' . get_permalink($first_post_excluded->ID) . '">' . $first_post_excluded->post_title . '</a>';
}

获取同一标签中的最后一篇文章,此示例检索与当前帖子位于同一标签中的最后一篇帖子。

$last_post_same_tag = get_boundary_post(true, '', false, 'post_tag');
if (!empty($last_post_same_tag)) {
    echo '<a href="' . get_permalink($last_post_same_tag->ID) . '">' . $last_post_same_tag->post_title . '</a>';
}

获得同一自定义分类中的第一篇文章,此示例检索与当前帖子相同的自定义分类法(例如“product_cat”)中的第一篇帖子。

$first_post_custom_taxonomy = get_boundary_post(true, '', true, 'product_cat');
if (!empty($first_post_custom_taxonomy)) {
    echo '<a href="' . get_permalink($first_post_custom_taxonomy->ID) . '">' . $first_post_custom_taxonomy->post_title . '</a>';
}

  推荐:WordPress函数get_next_post()获取下一篇文章

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数get_boundary_post()获取边界文章

转载请保留链接:https://www.pythonthree.com/wordpress-get-boundary-post/

滚动至顶部