返回列表 发布新帖
查看: 92|回复: 0

[代码技巧] 子比主题 – 右上角随机访问文章功能

[复制链接]
SunJu_FaceMall
社区贡献

315

主题

190

回帖

1万

积分

等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 524 颗
   贡献 : 1849 点
   金币 : 12 枚
Icon在线时间 : 1297 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2025-12-9

荣誉勋章

会员LV.1会员LV.2会员LV.3会员LV.4会员LV.5会员LV.6会员LV.7会员LV.8会员LV.9会员LV.10

风云·优秀版主

飞流名人堂成员

1

实名认证 手机认证 vip vip-year FLLTCN发表于 2025-10-10 17:06:52 | 查看全部 |阅读模式 浙江金华

资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
这是一款子比主题的右上角随机访问文章功能,利用的子比自带的颜色和跳转图标,给自己的右上角加一个随机文章还是比较美观的,喜欢的自行部署吧!

tengfei_down - 2025-10-10T170443.539.webp

代码部署


一共两个部分的代码,跟着我的教程走不会有问题!
func代码
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可!
  1. function register_random_post_redirect_endpoint() {
  2.     register_rest_route('wp/v2', '/random-post', array(
  3.         'methods' => 'GET',
  4.         'callback' => 'handle_random_post_redirect',
  5.         'permission_callback' => '__return_true'
  6.     ));
  7. }
  8. add_action('rest_api_init', 'register_random_post_redirect_endpoint');

  9. function handle_random_post_redirect($request) {
  10.     // 获取所有已发布的文章
  11.     $args = array(
  12.         'post_type' => 'post',
  13.         'post_status' => 'publish',
  14.         'fields' => 'ids',
  15.         'posts_per_page' => -1,
  16.     );
  17.    
  18.     $posts = get_posts($args);
  19.    
  20.     if (empty($posts)) {
  21.         return new WP_Error('no_posts', '没有找到文章', array('status' => 404));
  22.     }
  23.    
  24.     // 随机选择一篇文章
  25.     $random_post_id = $posts[array_rand($posts)];
  26.     $random_post_url = get_permalink($random_post_id);
  27.    
  28.     // 直接重定向
  29.     if (!headers_sent()) {
  30.         wp_redirect($random_post_url);
  31.         exit;
  32.     }
  33.    
  34.     // 如果无法重定向,返回JSON响应
  35.     return array(
  36.         'post_id' => $random_post_id,
  37.         'post_url' => $random_post_url,
  38.         'message' => '请手动访问上面的URL'
  39.     );
  40. }
  41. function zib_get_random_post_button($args = array()) {
  42.     if (!_pz('random_post_button', true)) {
  43.         return '';
  44.     }

  45.     $random_icon = '<svg t="1751112388719" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5410" width="1000" height="1000" data-spm-anchor-id="a313x.search_index.0.i12.4dfa3a81Mc8lKy"><path d="M166.464 448.704l212.64 128.736a32 32 0 1 1-33.152 54.72L79.424 470.848a32 32 0 0 1 4.256-56.928l832-347.456a32 32 0 0 1 43.744 35.52l-145.76 768a32 32 0 0 1-48.576 21.056L494.88 719.712a32 32 0 0 1 34.24-54.08l230.464 146.112L885.44 148.48 166.464 448.704z" p-id="5411" data-spm-anchor-id="a313x.search_index.0.i14.4dfa3a81Mc8lKy"></path><path d="M416 632.096V896a32 32 0 0 1-64 0V617.376a32 32 0 0 1 11.2-24.32L752.192 260a32 32 0 0 1 41.6 48.64L416 632.064z" p-id="5412" data-spm-anchor-id="a313x.search_index.0.i13.4dfa3a81Mc8lKy"></path></svg>';

  46.     $defaults = array(
  47.         'class' => 'jb-blue radius-circle',
  48.         'text' => '随机文章',
  49.         'icon' => $random_icon,
  50.         'tooltip' => '随机阅读一篇文章',
  51.         'echo' => true
  52.     );
  53.    
  54.     $args = wp_parse_args($args, $defaults);
  55.    
  56.     $button = '<a href="' . esc_url(home_url('/wp-json/wp/v2/random-post')) . '" rel="external nofollow"  
  57.           class="random-post-button but ' . esc_attr($args['class']) . ' ml10"
  58.           data-toggle="tooltip"
  59.           data-placement="bottom"
  60.           title="' . esc_attr($args['tooltip']) . '">'
  61.           . $args['icon']
  62.           . esc_html($args['text'])
  63.           . '</a>';

  64.     if ($args['echo']) {
  65.         echo $button;
  66.     }
  67.    
  68.     return $button;
  69. }
复制代码

zib-header.php代码
定位:/wp-content/themes/zibll/inc/functions/zib-header.php文件,我们到了这个文件之后搜代码
  1. function zib_menu_button($layout = 1)
复制代码
将下面的代码放到里面,如下图我圈的地方即可!
tengfei_down - 2025-10-10T170553.721.webp
  1. $random_button = zib_get_random_post_button(array(
  2.         'echo' => false
  3.     ));
  4.     if ($random_button) {
  5.         $button .= $random_button;
  6.     }
复制代码

角标颜色
可以看到我们func代码里面有一行'class' => 'jb-blue radius-circle',里面的jb-blue,我们可以按照下面来自己弄自己喜欢的颜色即可!
class
样式
class
样式
class
样式
c-red
红色文字
b-theme
主题背景色
jb-red
渐变红色背景
c-yellow
橙色文字
b-red
红色背景
jb-yellow
渐变橙色背景
c-blue
蓝色文字
b-yellow
橙色背景
jb-blue
渐变蓝色背景
c-blue-2
深蓝色文字
b-blue
蓝色背景
jb-green
渐变绿色背景
c-green
绿色文字
b-green
深蓝色背景
jb-purple
渐变紫色背景
c-purple
紫色文字
b-purple
紫色背景
jb-vip1
渐变金色背景


jb-vip2
渐变黑色背景
[/zhedie]

本帖被以下淘专辑推荐:

路虽远,行则将至;事虽难,做则必成。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
10-30 17:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
10-09 09:07
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-24 09:00
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-11 11:40
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-02 09:17
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-27 08:56
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-20 15:12
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-03 10:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
06-30 08:57
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
06-18 09:14
系统消息:清风网络已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-11 09:40
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-10 09:31
系统消息:IXM77777已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 13:44
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 09:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-09 08:52
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-08 09:24
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-07 09:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
02-27 09:35
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-26 09:06
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-25 08:49
站内通告

提供资源交易、信息共享、靓号交流、技术变现、学习问答、兴趣娱乐等全面服务。

1.丰富功能系统,扩展社区特色玩法,打造最好的互联网聚集圈子。

2.准确信息真实交易,安全快捷又方便,让虚拟交易面对面。

3. 天上不会掉馅饼,话术骗术迷人心,切勿脱离平台线下交易,被骗与平台无关!

4. 欺诈骗钱,违规违法将视情受到警告&禁言&封号甚至检举至👮🏻‍♀️处理!

官方Q群:123129钉推群:BAYR2383 站长QQ:3220000000

投诉/建议/商务合作联系

fl@fllt.cn

严禁私下交易,被骗与本站无关。
违反交易细则,取证立查严惩。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

👮曝光Ta|🧿小黑屋|📴手机页|飞流网 ( 渝ICP备2025054677号-1|电信增值许可 渝B2-20250789 )|网站地图

GMT+8, 2025-12-10 13:56 , Processed in 0.094035 second(s), 61 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表