🎅挂件馆🌟铭牌馆✨️靓标库🧚‍♂️名人堂🦄宝可梦🍎水果机🥊猜拳🏧黑市🏧银行💹抽奖
   🎁
返回列表 发布新帖
查看: 199|回复: 0

[代码技巧] 子比主题 – 导航栏菜单显示分类文章数量

[复制链接]
起源之星
任务·年会员
累计签到LV.3
摩羯座
+1
Top炫彩
00001
FLLTCN
社区贡献
344 214 23759
等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 196 颗
   贡献 : 13324 点
   金币 : 4 枚
Icon在线时间 : 1918 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2026-7-21

荣誉勋章

起源之星任务·年会员累计签到LV.3摩羯座

送礼物:3 收礼物:1
Top炫彩
00001
FLLTCN 实名认证 特邀大神 诚信商家 信誉担保 精华作者+ 官方团队 vip vip-year 发表于 2025-10-8 20:13:57 | 查看全部 |阅读模式 浙江金华

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

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

×
给子比主题美化并设置导航栏菜单,特别展示如何添加并更新今日更新文章数量的显示功能,让你的网站导航更加动态与吸引用户,喜欢的自行部署,这款我改了一些代码,很多站都是搬运的,都是(数量)这种篇文章显示
tengfei_down (29).webp
[h1]代码部署[/h1]
func代码
我们到:/wp-content/themes/zibll/func.php里面,没有这个文件自己创一个,记得加上php头,要不然报错,将下面的代码放到里面
  1. class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
  2.     // 开始输出每个菜单项
  3.     function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
  4.         // 和默认 Walker 一样处理元素的基础部分
  5.         $classes = empty($item->classes) ? array() : (array) $item->classes;
  6.         $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
  7.         $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

  8.         $id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item);
  9.         $id = $id ? ' id="' . esc_attr($id) . '"' : '';

  10.         $output .= '<li' . $id . $class_names .'>';

  11.         $atts = array();
  12.         $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
  13.         $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
  14.         $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
  15.         $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

  16.         $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args);

  17.         $attributes = '';
  18.         foreach ( $atts as $attr => $value ) {
  19.             if ( ! empty( $value ) ) {
  20.                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  21.                 $attributes .= ' ' . $attr . '="' . $value . '"';
  22.             }
  23.         }

  24.         $item_output = $args->before;
  25.         $item_output .= '<a'. $attributes .'>';
  26.         $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

  27.         // 添加分类项的文章数量
  28.         if ($item->object == 'category') {
  29.             $category_id = $item->object_id;
  30.             $category = get_category($category_id);
  31.             if ($category) {
  32.                 $count = $category->count;
  33.                 // 修改部分:将文章数量显示为 <badge> 标签
  34.                 $item_output .= ' <badge class="jb-red badge-bw">+' . $count . '</badge>';
  35.             }
  36.         }

  37.         $item_output .= '</a>';
  38.         $item_output .= $args->after;

  39.         $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  40.     }
  41. }
复制代码

zib-header.php代码
我们到:/wp-content/themes/zibll/func.php里面,没有这个文件自己创一个,记得加上php头,要不然报错,将下面的代码放到里面
  1. class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
  2.     // 开始输出每个菜单项
  3.     function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
  4.         // 和默认 Walker 一样处理元素的基础部分
  5.         $classes = empty($item->classes) ? array() : (array) $item->classes;
  6.         $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
  7.         $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

  8.         $id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item);
  9.         $id = $id ? ' id="' . esc_attr($id) . '"' : '';

  10.         $output .= '<li' . $id . $class_names .'>';

  11.         $atts = array();
  12.         $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
  13.         $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
  14.         $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
  15.         $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

  16.         $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args);

  17.         $attributes = '';
  18.         foreach ( $atts as $attr => $value ) {
  19.             if ( ! empty( $value ) ) {
  20.                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  21.                 $attributes .= ' ' . $attr . '="' . $value . '"';
  22.             }
  23.         }

  24.         $item_output = $args->before;
  25.         $item_output .= '<a'. $attributes .'>';
  26.         $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

  27.         // 添加分类项的文章数量
  28.         if ($item->object == 'category') {
  29.             $category_id = $item->object_id;
  30.             $category = get_category($category_id);
  31.             if ($category) {
  32.                 $count = $category->count;
  33.                 // 修改部分:将文章数量显示为 <badge> 标签
  34.                 $item_output .= ' <badge class="jb-red badge-bw">+' . $count . '</badge>';
  35.             }
  36.         }

  37.         $item_output .= '</a>';
  38.         $item_output .= $args->after;

  39.         $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  40.     }
  41. }
复制代码

本帖被以下淘专辑推荐:

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

本版积分规则

飞流广播+ 发布

飞流这论坛到底有没有资本给我投资一下啊!
07-20 22:07
系统消息:飞流安卓客户端APP已上线,请在QQ群(123129)群文件内获取下载。
02-21 02:22
01-24 12:40
站内通告

📢 六月份后均不在线,请悉知

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

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

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

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

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

官方Q群:1003810038钉推群:BAYR2383 站长QQ:3388700000

飞流论坛(FLLT.CN),一个专注于资源信息发布、虚拟网络交易、技术学习与娱乐社交的交流平台。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

💥客户端|🧿小黑屋|📴手机页|飞流网 |网站地图

GMT+8, 2026-7-21 00:51 , Processed in 0.067011 second(s), 71 queries, MemCached On , Gzip On.

飞流论坛 HanAnalytics icp

Copyright © 2024-2026 飞流网 版权所有 All Rights Reserved. X3.5

快速回复 返回顶部 返回列表