WordPress – 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
1 在文章内容末尾添加随机推荐文章
在文章末尾增加随机推荐文章既可以增加PV也有利于SEO,研究了一番之后,得出了纯代码在文章内容末尾增加10个随机推荐文章链接的方法。
只需要在主题的function.php文件中添加以下代码:
function RandomArticle($content )
{
//global $post;
if(is_single())
{
// 获取文章分类
$categories = get_the_category();
foreach($categories as $category)
{
// 获取文章分类ID
$catid = $category->term_id;
// 随机获取当前分类10个随机文章
$posts = get_posts('numberposts=10&orderby=rand&category='. $catid);
$randomArticle = '';
foreach($posts as $post)
{
// 获取文章标题
$postTitle = $post->post_title;
// 获取文章链接
$postLink = $post->guid;
$link = '<li><a href="'.$postLink.'">'.$postTitle.'</a></li>';
$randomArticle = $randomArticle.$link;
}
$randomArticle = '<h1>随机文章推荐</h1><ul>'.$randomArticle.'</ul>';
}
$content = $content.'<div class="post-random-article" >'.$randomArticle.'</div>';
}
return $content;
}
add_action( 'the_content', 'RandomArticle' );
效果如本文下随机推荐文章所示。
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:WordPress – 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
原文链接:https://www.stubbornhuang.com/1015/
发布于:2020年12月09日 21:33:50
修改于:2023年06月26日 22:02:19
当前分类随机文章推荐
- WordPress - get_edit_post_link函数详解 阅读1349次,点赞0次
- WordPress - 网站性能优化,延迟加载css和js文件 阅读154次,点赞0次
- WordPress - WordPress后台登录设置验证码,防止恶意爆破网站 阅读4231次,点赞0次
- WordPress - Windows使用PhpStudy本地部署WordPress 阅读4070次,点赞0次
- 网站个性化 - 添加人形时钟 honehone_clock.js 阅读3333次,点赞0次
- WordPress - 查看别人的网站是用的WordPress的哪个主题 阅读3379次,点赞0次
- WordPress - 获取某个用户发表的文章数量 阅读2268次,点赞0次
- WordPress - 使用Cravatar替换Gravatar提供头像服务 阅读1249次,点赞0次
- WordPress - 修改管理后台登录地址,防止恶意爆破 阅读3087次,点赞0次
- WordPress - get_sidebar函数,加载主题侧边栏模板 阅读1111次,点赞0次
全站随机文章推荐
- 资源分享 - Computational Geometry on Surfaces - Performing Computational Geometry on the Cylinder, the Sphere, the Torus, and the Cone 英文高清PDF下载 阅读2240次,点赞0次
- Google Adsense - 从Google Adsense开通到第一个10美元我用了一年时间 阅读2366次,点赞2次
- 资源分享 - Advanced High Dynamic Range Imaging, First Edition 英文高清PDF下载 阅读1856次,点赞0次
- 资源分享 - Simulating Humans - Computer Graphics Animation and Control 英文高清PDF下载 阅读1434次,点赞0次
- 工具软件 - 使用Potplayer录制在线直播视频流 阅读1085次,点赞0次
- TensorRT - 使用C++ SDK出现无法解析的外部符号 "class sample::Logger sample::gLogger"错误 阅读678次,点赞0次
- 资源分享 - Vulkan Programming Guide - The Official Guide to Learning Vulkan 英文高清PDF下载 阅读3360次,点赞0次
- Linux - 创建软链接、删除软链接、修改软链接 阅读47次,点赞0次
- C++STL容器 - std::map删除指定元素 阅读2606次,点赞0次
- nginx - 禁止特定User-Agent或者空User-Agent等垃圾爬虫爬取网站、爆破接口 阅读42次,点赞0次
评论
169