注册时间2024-11-22
最后登录2025-10-29
在线时间1294 小时
UID1
买家信用
卖家信用
风云·优秀版主
|
资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!
您需要 登录 才可以下载或查看,没有账号?立即注册
×
给子比主题美化并设置导航栏菜单,特别展示如何添加并更新今日更新文章数量的显示功能,让你的网站导航更加动态与吸引用户,喜欢的自行部署,这款我改了一些代码,很多站都是搬运的,都是(数量)这种篇文章显示
[h1]代码部署[/h1]
func代码 我们到:/wp-content/themes/zibll/func.php里面,没有这个文件自己创一个,记得加上php头,要不然报错,将下面的代码放到里面
- class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
- // 开始输出每个菜单项
- function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
- // 和默认 Walker 一样处理元素的基础部分
- $classes = empty($item->classes) ? array() : (array) $item->classes;
- $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
- $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
- $id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item);
- $id = $id ? ' id="' . esc_attr($id) . '"' : '';
- $output .= '<li' . $id . $class_names .'>';
- $atts = array();
- $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
- $atts['target'] = ! empty( $item->target ) ? $item->target : '';
- $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
- $atts['href'] = ! empty( $item->url ) ? $item->url : '';
- $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args);
- $attributes = '';
- foreach ( $atts as $attr => $value ) {
- if ( ! empty( $value ) ) {
- $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
- $attributes .= ' ' . $attr . '="' . $value . '"';
- }
- }
- $item_output = $args->before;
- $item_output .= '<a'. $attributes .'>';
- $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
- // 添加分类项的文章数量
- if ($item->object == 'category') {
- $category_id = $item->object_id;
- $category = get_category($category_id);
- if ($category) {
- $count = $category->count;
- // 修改部分:将文章数量显示为 <badge> 标签
- $item_output .= ' <badge class="jb-red badge-bw">+' . $count . '</badge>';
- }
- }
- $item_output .= '</a>';
- $item_output .= $args->after;
- $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
- }
- }
复制代码
zib-header.php代码 我们到:/wp-content/themes/zibll/func.php里面,没有这个文件自己创一个,记得加上php头,要不然报错,将下面的代码放到里面
- class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
- // 开始输出每个菜单项
- function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
- // 和默认 Walker 一样处理元素的基础部分
- $classes = empty($item->classes) ? array() : (array) $item->classes;
- $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
- $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
- $id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item);
- $id = $id ? ' id="' . esc_attr($id) . '"' : '';
- $output .= '<li' . $id . $class_names .'>';
- $atts = array();
- $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
- $atts['target'] = ! empty( $item->target ) ? $item->target : '';
- $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
- $atts['href'] = ! empty( $item->url ) ? $item->url : '';
- $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args);
- $attributes = '';
- foreach ( $atts as $attr => $value ) {
- if ( ! empty( $value ) ) {
- $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
- $attributes .= ' ' . $attr . '="' . $value . '"';
- }
- }
- $item_output = $args->before;
- $item_output .= '<a'. $attributes .'>';
- $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
- // 添加分类项的文章数量
- if ($item->object == 'category') {
- $category_id = $item->object_id;
- $category = get_category($category_id);
- if ($category) {
- $count = $category->count;
- // 修改部分:将文章数量显示为 <badge> 标签
- $item_output .= ' <badge class="jb-red badge-bw">+' . $count . '</badge>';
- }
- }
- $item_output .= '</a>';
- $item_output .= $args->after;
- $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
- }
- }
复制代码
|
|