应贝总要求, 在博文后面加入了 上一篇, 和下一篇博文的链接. 下一篇是比较新的 上一篇比较旧(时间比较早). 需要用到WP的两个函数, get_previous_post 和 get_next_post. 需要通过 add_filter 对文章内容加上了过滤器 (在文章最后面/最前面) 添加上下博文链接.
直接上PHP代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | add_filter( 'the_content', 'show_next_prev_post_links' ); function show_next_prev_post_links($content) { $pages = ''; if (is_single()) { // 只在文章页显示 $prev_post = get_previous_post(); if (!empty( $prev_post )) { // 上一篇博文 比较旧 $pages .= "上一篇博文: <a href='/archives/".$prev_post->ID."'>$prev_post->post_title</a>"; } $next_post = get_next_post(); if (!empty( $next_post )) { // 下一篇博文 比较新 $pages .= " | 下一篇博文: <a href='/archives/".$next_post->ID."'>$next_post->post_title</a>"; } } return $content . $pages; } |
add_filter( 'the_content', 'show_next_prev_post_links' ); function show_next_prev_post_links($content) { $pages = ''; if (is_single()) { // 只在文章页显示 $prev_post = get_previous_post(); if (!empty( $prev_post )) { // 上一篇博文 比较旧 $pages .= "上一篇博文: <a href='/archives/".$prev_post->ID."'>$prev_post->post_title</a>"; } $next_post = get_next_post(); if (!empty( $next_post )) { // 下一篇博文 比较新 $pages .= " | 下一篇博文: <a href='/archives/".$next_post->ID."'>$next_post->post_title</a>"; } } return $content . $pages; }
把上面代码 加入到 子主题 模块文件 functions.php 的最后即可. 清除缓存 WordPress 和 Cloudflare 的即可. 需要注意的是 get_previous_post 和 get_next_post 只对文章有效, 对页面无效, 所以需要用 is_single 来判断是否是文章页.
需要注意的是 得根据情况 修改 上面代码中 上下博文的链接, 主要有三种方式:
- guid
- ID
- post_title
懂HTML的人很多 我就不多说了. 这也是增加站点内链的一种方式, 兴许会有利于 搜索引擎优化呢. 贝总, 我对你是真爱啊.
英文: How to Add Next and Previous Posts Links in WordPress?
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK