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

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

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

  在 WordPress CMS内容管理系统中,get_next_post()是一个内置函数,用于检索与当前帖子相邻且具有相同帖子类型的下一篇帖子。它通常用于 WordPress 模板文件中的循环上下文中,以在博客页面上显示指向下一篇文章的链接。

  推荐:如何更改WordPress时区

get_next_post()函数基本语法

描述

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

用法

<?php get_next_post( $in_same_term, $excluded_terms, $taxonomy ) ?>

参数

  $in_same_term

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

  $excluded_terms

 (int[]|string)(可选)要排除的分类ID。默认值: ”

  $taxonomy

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

返回值

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

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

get_next_post()函数示例

  链接到同一分类中的上一篇文章,文本标题为文章标题。(源文件可参考这里

<?php 
$next_post = get_next_post(); 
if (!empty( $next_post )): ?> 
  <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a> 
<?php endif; ?>

  推荐:Redis Object Cache Pro插件下载WordPress对象缓存插件

如何使用get_next_post()

  将函数get_next_post添加到 functions.php文件的示例:

function my_get_next_post_link() {
	$next_post = get_next_post();
	if ( $next_post ) {
		return '<a href="' . esc_url( get_permalink( $next_post->ID ) ) . '">' . esc_html( get_the_title( $next_post->ID ) ) . '</a>';
	}
}

  推荐:WooCommerce Dropshipping免费下载WordPress Dropshipping插件

  推荐:WordPress函数使用手册

5/5 - (1 vote)

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

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

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部