子比主题 – 导航栏菜单显示分类文章数量
给子比主题美化并设置导航栏菜单,特别展示如何添加并更新今日更新文章数量的显示功能,让你的网站导航更加动态与吸引用户,喜欢的自行部署,这款我改了一些代码,很多站都是搬运的,都是(数量)这种篇文章显示代码部署
我们到:/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);
}
}
我们到:/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);
}
}
页:
[1]