next_post_link()
函数是常用的Wordpress函数之一,用于显示当前文章相邻的下一篇文章链接,上一篇、下一篇文章几乎是当下网站都会添加的文章页面元素,据说是有利于SEO优化。本篇内容博客吧分享获取下一篇文章函数。
函数结构
<?php next_post_link( string $format = '%link »', string $link = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', string $taxonomy = 'category' ) ?>
参数说明
$format – (字符串)(可选)链接锚的格式。默认 ‘« %link’。默认值:’%link »’
$link – (字符串)(可选)链接的文本。默认“%title”。默认值:’%title’
$in_same_term – (布尔值)(可选)是否调用相同分类的链接。默认值:false
$excluded_terms – (数组或字符串)(可选)要排除的分类ID,多个ID用数组或者英文逗号的形式隔开。默认值: ”
taxonomy–(字符串)(可选)分类法,如果in_same_term 的值为 true 时有效。默认值:“category”
使用示例
示例一
如果存在下一篇文章,则带箭头的标题超链接的形式显示下一篇文章,如果没有下一篇文章,则不显示任何内容。
<?php next_post_link();?>
示例二
去掉默认的箭头
<?php next_post_link('%link','%title');?>
示例三
去掉默认的箭头,不使用标题文字,加粗
<?php next_post_link('<strong>%link</strong>','下一篇');?>
示例四
只显示相同分类的下一篇文章
<?php next_post_link('%link','%title',true,'','category');?>
示例五
排除分类ID为5的文章
<?php next_post_link('%link','%title','','5');?>
示例六
排除分类ID为5,6,7的文章
<?php next_post_link('%link','%title','',array(5,6,7));?>
函数源代码的位置:wp-includes/link-template.php
官方文档:https://developer.wordpress.org/reference/functions/next_post_link/
文章版权声明:除非注明,否则均为老余个人博客原创文章,转载或复制请以超链接形式并注明出处。
发表评论