WordPress – 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
原创文章,作者:StubbornHuang,如若转载,请注明出处:《WordPress – 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章》https://www.stubbornhuang.com/1015/
1 在文章内容末尾添加随机推荐文章
在文章末尾增加随机推荐文章既可以增加PV也有利于SEO,研究了一番之后,得出了纯代码在文章内容末尾增加10个随机推荐文章链接的方法。
只需要在主题的function.php文件中添加以下代码:
function RandomArticle(content )
{
//globalpost;
if(is_single())
{
// 获取文章分类
categories = get_the_category();
foreach(categories as category)
{
// 获取文章分类IDcatid = category->term_id;
// 随机获取当前分类10个随机文章posts = get_posts('numberposts=10&orderby=rand&category='. catid);randomArticle = '';
foreach(posts aspost)
{
// 获取文章标题
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>';
}
returncontent;
}
add_action( 'the_content', 'RandomArticle' );
效果如本文下随机推荐文章所示。
当前分类随机文章推荐
- WordPress - 查看别人的网站是用的WordPress的哪个主题
- WordPress - WordPress后台登录设置验证码,防止恶意爆破网站
- WordPress - 通过Server酱发送微信通知给微信
- WordPress - robots.txt
- WordPress - 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
- WordPress - 利用Revolvermaps在首页实时显示访客3D地球
- WordPress - 使用插件的方式作出类似向主题function.php中添加代码的功能
- WordPress - 插件OSS Upload与WP Editor.md/WP Githuber MD插件冲突,导致katex公式不正确显示的问题
- WordPress - 增加百度统计代码
- WordPress - 禁用XML-RPC接口,禁止访问xmlrpc.php,避免DDOS攻击,防止暴力破解
全站随机文章推荐
- Modern OpenGL - GLSL着色语言3:GLSL中的数据类型
- Python3 - 正则表达式去除字符串中的特殊符号
- 姿态估计之human3.6m数据集骨骼关节keypoint标注对应
- Python3爬虫 - requests的请求响应状态码(requests.status_code)
- Unity - Color32[]转为byte[]字节数组
- 资源分享 - OpenGL编程指南(原书第9版)- OpenGL红宝书高清带书签PDF下载
- OpenGL地球与太阳绕转代码
- 资源分享 - 非均匀有理B样条(第二版,Les Piegl),The NURBS Book中文译文版下载
- OpenCV - 新建一个图片,并在图片上画由一点到另一点的直线,采用反走样形式
- 资源分享 - PHP与MySQL程序设计(第4版) 中文 PDF下载