^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

WordPress实现随机文章

一个类似于 google 搜索中的“手气不错”的功能。如果你想实现此功能,需要你稍微懂一点编程技术。本文提供的代码只是作为基本的实现思路,并不是一个完美的代码。

功能描述

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

点击“手气不错”或“随机推荐”的类似按钮,为网友随机推荐博客中的文章,目的为了更好的增加浏览者的体验。

实现思路

利用 wordpress 中两个内置函数 get_posts()the_permalink();

get_posts() : 获取文章

numberposts= 1 获取一篇文章

rderby=rand 随机文章

the_permalink() : 当前文章链接

示例地址

https://dedewp.com/1-2

代码分享

<?php /* Template Name: random(随机推荐) */ >
<?php $rand_post=get_posts('numberposts=1&orderby=rand'); ?>
<html>
<<span id="9_nwp" style="width: auto; height: auto; float: none;"><a id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.phpc=news&cf=1001&ch=0&di=128&fv=17&jk=213ae9ee57972c6&k=head&k0=head&kdi0=0&luki=5&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=c67279e59eae1302&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F2134%2Ehtml&urlid=0" target="_blank" mpid="9" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">head</span></a></span>>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php foreach($rand_post as $post) : >
<meta http-equiv="refresh" content="2;url='<?php the_permalink();>';">
<?php endforeach; ?>
<title>随机推荐中,请稍候...</title>
<<span id="10_nwp" style="width: auto; height: auto; float: none;"><a id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.phpc=news&cf=1001&ch=0&di=128&fv=17&jk=213ae9ee57972c6&k=style&k0=style&kdi0=0&luki=1&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=c67279e59eae1302&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F2134%2Ehtml&urlid=0" target="_blank" mpid="10" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">style</span></a></span>>
body{background:#000}.loading{-webkit-animation:fadein 2s;-moz-animation:fadein 2s;-o-animation:fadein 2s;animation:fadein 2s}@-moz-keyframes fadein{from{opacity:0}to{opacity:1}}@-webkit-keyframes fadein{from{opacity:0}to{opacity:1}}@-o-keyframes fadein{from{opacity:0}to{opacity:1}}@keyframes fadein{from{opacity:0}to{opacity:1}}.spinner-wrapper{position:absolute;top:0;left:0;z-index:300;height:100%;min-width:100%;min-height:100%;background:rgba(255,255,255,0.93)}.spinner-text{position:absolute;top:50%;left:50%;margin-left:-90px;margin-top: 2px;color:#BBB;letter-spacing:1px;font-weight:700;font-size:36px;font-family:Arial}.spinner{position:absolute;top:50%;left:50%;display:block;margin-left:-160px;width:1px;height:1px;border:25px solid rgba(100,100,100,0.2);-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;border-left-color:transparent;border-right-color:transparent;-webkit-animation:spin 1.5s infinite;-moz-animation:spin 1.5s infinite;animation:spin 1.5s infinite}@-webkit-keyframes spin{0%,100%{-webkit-transform:rotate(0deg) scale(1)}50%{-webkit-transform:rotate(720deg) scale(0.6)}}@-moz-keyframes spin{0%,100%{-moz-transform:rotate(0deg) scale(1)}50%{-moz-transform:rotate(720deg) scale(0.6)}}@-o-keyframes spin{0%,100%{-o-transform:rotate(0deg) scale(1)}50%{-o-transform:rotate(720deg) scale(0.6)}}@keyframes spin{0%,100%{transform:rotate(0deg) scale(1)}50%{transform:rotate(720deg) scale(0.6)}}
</<span id="11_nwp" style="width: auto; height: auto; float: none;"><a id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.phpc=news&cf=1001&ch=0&di=128&fv=17&jk=213ae9ee57972c6&k=style&k0=style&kdi0=0&luki=1&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=c67279e59eae1302&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F2134%2Ehtml&urlid=0" target="_blank" mpid="11" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">style</span></a></span>>
</<span id="12_nwp" style="width: auto; height: auto; float: none;"><a id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.phpc=news&cf=1001&ch=0&di=128&fv=17&jk=213ae9ee57972c6&k=head&k0=head&kdi0=0&luki=5&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=c67279e59eae1302&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F2134%2Ehtml&urlid=0" target="_blank" mpid="12" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">head</span></a></span>>
<body>
<div class="loading">
 <div class="spinner-wrapper">
 <<span id="13_nwp" style="width: auto; height: auto; float: none;"><a id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.phpc=news&cf=1001&ch=0&di=128&fv=17&jk=213ae9ee57972c6&k=span&k0=span&kdi0=0&luki=3&n=10&p=baidu&q=06003100_cpr&rb=0&rs=1&seller_id=1&sid=c67279e59eae1302&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1948625&u=http%3A%2F%2Fwuzhuti%2Ecn%2F2134%2Ehtml&urlid=0" target="_blank" mpid="13" style="text-decoration: none;"><span style="color:#0000ff;font-size:12px;width:auto;height:auto;float:none;">span</span></a></span> class="spinner-text">随机推荐,推荐中...</span>
 <span class="spinner"></span>
 </div>
</div>
</body>
</html>

在主题目录下新建一个 random.php 文件,将以上代码粘贴到文件中,作为随机推荐 url(页面)的模板代码使用,通过后台创建页面,选择random.php作为模板。

WordPress实现随机文章

通过访问新建页面的 url实现随机文章推荐功能。

赠人玫瑰,手有余香。