WordPress技巧:菜单显示每日更新数量

将下列代码放置在 WordPress 主题模板函数 functions.php 中:

注册菜单一步若主题中已存在可无视之。

// 注册菜单 register_nav_menus( array(  'primary' => __( '我的菜单' ), ) ); // 强化菜单 调用代码 (php) wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new description_walker ) ); (/php) // 强化菜单 结构 class description_walker extends Walker_Nav_Menu {  function start_el(&$output, $item, $depth, $args)  {   global $wp_query;   $indent = ( $depth ) ? str_repeat( "/t", $depth ) : '';     $class_names = $value = '';     $classes = empty( $item->classes ) ? array() : (array) $item->classes;     $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );   $class_names = ' class="'. esc_attr( $class_names ) . '"';     $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';     $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';   $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';   $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';   $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';     $prepend = '<span>';   $append = '</span>';   $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';     if($depth != 0)   {    $description = $append = $prepend = "";   }     $item_output = $args->before;   $item_output .= '<a'. $attributes .'>';   $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;   // seabye++   if ( $item->description<=0 ) { $item_output .= $description; }    else { $item_output .= '<span class="day">+'.get_this_week_post_count_by_category($item->description).'</span>'; }   $item_output .= $args->link_after;   // seabye++ end   // $item_output .= $description.$args->link_after;   $item_output .= '</a>';   $item_output .= $args->after;     $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );  } } // 强化菜单 代表时间 // 一日 today // 一周 1 week ago // 一年 1 year ago // 千年 1000 year ago function get_this_week_post_count_by_category($id) {  $date_query = array(   array(    'after' => 'today'    )   );  $tax_query = array(   array(    'taxonomy' => 'category',     'field' => 'id',     'terms' => $id    )   );  $args = array(   'post_type' => 'post',   'post_status' => 'publish',   'tax_query' => $tax_query,   'date_query' => $date_query,   'no_found_rows' => true,   'suppress_filters' => true,   'fields' => 'ids',   'posts_per_page' => -1   );  $query = new WP_Query( $args );  return $query->post_count; }

如何调用:

红色加粗为需要添加的内容。

<?php wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new description_walker ) ); ?>

如何添加:

菜单 – 显示选项,勾出 “图像描述”,在项目的 “图像描述” 中添加需要显示更新数目的分类 id。

WordPress技巧:菜单显示每日更新数量

B站样式:

.day{position:absolute;top:-10px;display:block;left:50%;color:#fff;border-radius:4px;line-height:14px;padding:1px 5px 1px 5px;text-align:center;font-family:Calibri;background:#f489ad;font-size:12px;min-width:18px;}
本文所提供的资源来自网友分享,仅供学习交流,请勿用于商业用途。
站长萌萌哒 » WordPress技巧:菜单显示每日更新数量

发表回复

优质站长资源尽在站长萌萌哒

立即登录 免费注册
[erphp_weixin_scan]