WordPress 点击按钮打开随机文章功能实现代码,无需插件。
admin 阅读:109 2024-03-14
如题,使用源码的方式实现点击按钮打开随机文章。具体步骤如下:
新建名为page-random.php的文件,输入如下代码,以utf-8编码保存。
<?php $rand_post=get_posts('numberposts=1&orderby=rand'); foreach($rand_post as $post) : ?> <script> location="<?php the_permalink(); ?>";</script> <?php endforeach; ?>将这个文件上传到当前主题目录下。
登录管理后台,新建页面,页面别名:random。
然后在需要的地方使用a标签调用这个页面的 url 即可。比如我将这个页面的 url 设置成了:https://everycountry.xyz/random/,那么我的按钮代码:
<a href="https://everycountry.xyz/random/">随机看看</a>
声明
1、部分文章来源于网络,仅作为参考。 2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!



