File: /www/wwwroot/www.scdc-marine.com/wp-content/themes/scdc-marine/HekuClass/View/Top_Walker_Menu.php
<?php
namespace Heku\HekuClass\View;
/**
* @package Heku\HekuClass\View
* @subpackage Classes/Top_Walker_Menu
* @author Heku Daleconan <http://www.daleconan.com>
* @copyright Copyright (c) 2022-2023, Heku Daleconan
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class Top_Walker_Menu extends \Walker_Nav_Menu {
public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
/**
* 主列表包裹 li 标签 开始
* <li class=""> <span> </span>
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth * 2 + $this->numtab() ) ;
// 默认的 li 标签类。会被过滤器 过滤 apply_filters( 'nav_menu_css_class',"")
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$current = array( "menu-item" , "current-menu-item" , "menu-item-has-children" );
$classes = array_intersect( $classes , $current );
//添加 第一层子菜单的类
//var_dump($args->walker);
if($args->walker->has_children && $depth == 0){
$child_item_type = heku_get_menu_option( $item->ID , 'menu_type' );
if($child_item_type){
$classes[] = 'heku-'.$child_item_type;
}
}
//添加菜单深度的类
$classes[] = 'depth-' . $depth;
if( $depth == 1 ){
$classes[] = 'wow fadeInDown';
}
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
// 第一层 </t>(n)<li class="">
if($item->menu_item_parent == 0 && $depth == 0){
if($item->menu_order == 1){
$output .= "{$n}{$indent}<li$class_names>";
}else{
$output .= "{$t}<li$class_names>";
}
}else{
$output .= "{$indent}<li$class_names>";
}
$menu_type = heku_get_menu_option( $item->menu_item_parent , 'menu_type' );
$menu_img = heku_get_menu_option( $item->ID , 'sidebar_menu_img' );
//var_dump($menu_img); //var_dump($menu_type);
// 生成 a 标签的 具体参数,标题等包裹参数
$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 : '';
if( $depth == 1 ){
$atts['class'] = 'wow fadeInDown';
}
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter 标签 $title 内容 */
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
if($depth == 0){
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . $args->link_after;
if($args->walker->has_children){
$item_output .= "</a>{$n}";
}else{
$item_output .= "</a>";
}
}elseif($depth == 1){
// <a 标签前段部分 <a title="" target="" rel="" href=""> <img src="" alt=" " width=" "> <span> $title </span> <i class="" ></i></a>
// 列表展示样式 一 额外的展示内容
if($menu_type == 'menu-type1'){
//$item_output .= $this->menu_1($item) ;
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
if( $menu_img ){
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
$item_output .= "{$n}{$indent}{$t}<div class='sidebar-menu-li'>";
$item_output .= '<img class="sidebar-cat-img " src="'. $menu_img .'" alt="'. $title .'" width="100%">';
$item_output .= '<a'. $attributes .' class="sidebar-single-link" '.'>';
$item_output .= $args->link_before . '<span>'. $title .'</span>'. $args->link_after;
if($args->walker->has_children){
$item_output .= "</a>";
$item_output .= '<span class="icon iconfont"></span>';
$item_output .= "</div>{$n}";
}else{
$item_output .= "</a>";
$item_output .= "</div>{$n}";
}
}else{
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
$item_output .= '<a'. $attributes .' class="sidebar-single-link" '.'>';
// $item_output .= '<img class="sidebar-cat-img" src="'. $menu_img .'" width="100%">';
$item_output .= $args->link_before . '<span>'. $title .'</span>'. $args->link_after;
if($args->walker->has_children){
$item_output .= "</a>{$n}";
}else{
$item_output .= "</a>";
}
}
}
// 列表展示样式 二, 额外的展示内容
if($menu_type == 'menu-type2'){
$item_output .= $this->menu_2($item) ;
$item_output .= "{$indent}";
}
// 列表展示样式 三 额外的展示内容
if($menu_type == 'menu-type3'){
//$item_output .= $this->menu_3($item) ;
}
}elseif($depth == 2){
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= "</a>";
}else{
// <a 标签前段部分 <a title="" target="" rel="" href=""> $title </a>
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= "</a>";
}
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* 主列表包裹 li 标签 结束
* </li>
*/
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
}else{
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth * 2 + $this->numtab() - 1 ) ;
$indent2 = str_repeat( $t, $depth * 2 + $this->numtab() ) ;
if( $depth == 0 ){
if(in_array('menu-item-has-children',$item->classes)){
$output .= "</li>{$n}$indent";
}else{
$output .= "</li>{$n}$indent";
}
}elseif( $depth > 0 ){
if(in_array('menu-item-has-children',$item->classes)){
$output .= "$indent2</li>{$n}";
}else{
$output .= "</li>{$n}";
}
}else{
$output .= "</li>{$n}";
}
}
/**
* 子分类的 包裹 ul 标签 开始
* <ul class ="sub-menu">
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
}else{
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth * 2 + $this->numtab() + 1 );
$classes = array();
// 第一层的 ul 层 的class类
if( $args->walker->has_children ){
$classes = array( 'sub-menu' , 'sub-menu-' . $depth);
}
$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$class_ul_names = ' class="container-2 clearfix"';
if( $args->walker->has_children && $depth == 0){
$output .= "{$indent}<div$class_names>{$n}";
$output .= "{$indent}<ul$class_ul_names>{$n}";
}elseif( $depth == 1){
$output .= "{$indent}<div$class_names>{$n}";
$output .= "{$indent}<ul$class_ul_names>{$n}";
}else{
$output .= "{$indent}<ul$class_names>{$n}";
}
}
/**
* 子分类的 包裹 ul 标签 结束
* </ul><!-- end ul 标签 -->
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
}else{
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth * 2 + $this->numtab() + 1 );
$indent2 = str_repeat( $t, $depth * 2 + $this->numtab() );
if( $depth == 0){
// $output .= "{$indent}<ul$class_names><div class='container clearfix'><div class='row no-gutters align-items-center justify-content-between'>{$n}";
$output .= "{$indent}</ul>{$n}";
$output .= "{$indent}</div>{$n}{$indent2}";
}elseif( $depth == 1){
// $output .= "{$indent}<ul$class_names><div class='container clearfix'><div class='row no-gutters align-items-center justify-content-between'>{$n}";
$output .= "{$indent}</ul>{$n}";
$output .= "{$indent}</div>{$n}{$indent2}";
}else{
$output .= "{$indent}</ul>{$n}{$indent2}";
}
}
/**
* 为了输出 美观,空格项目,或是制表隔数量
* (</t></t></t></t>)n
*/
public function numtab(){
$numtab = 6;
return $numtab;
}
public function menu_1($item){
$t = "\t";
$n = "\n";
$indent = str_repeat( $t, 1 * 2 + $this->numtab() + 1 );
$indent2 = str_repeat( $t, 1 * 2 + $this->numtab() + 2 );
if(!isset($item->_menu_item_object)) return;
$type = $this->get_post_type($item->_menu_item_object);
$sub_menu_type = heku_get_menu_option( $item->ID , 'sub_menu_type' );
$args = array(
'post_type' => $type,
'posts_per_page' => 2,
'paged' => 0,
'no_found_rows' => true
);
if($type == 'post' && isset($item->object_id)){
$args['cat'] = $item->object_id;
}
$the_query = new \WP_Query( $args );
$html = '';
if($the_query->have_posts()){
$class_names = ' class="sub-menu sub-menu-1"';
$sss = ' class="container clearfix list-style01 py-3 '. $sub_menu_type . ' "';
$html .= "{$n}{$indent}<ul$class_names>";
$html .= "<div$sss>{$n}";
wp_reset_postdata();
while( $the_query->have_posts() ){
$the_query->the_post();
$link = get_permalink();
$title = get_the_title();
$imgs = get_post_meta( get_the_ID(), 'banner_fliter_group2', true );
$class_nameli = ' class="news-item row no-gutters align-items-center clear px-2 px-3 wow fadeInUp"';
//$thumb = b2_get_thumb(array('thumb'=>Post::get_post_thumb($the_query->post->ID),'type'=>'fill','width'=>$size['width'],'height'=>$size['height']));
$html .= "{$indent2}<li$class_nameli>";
$html .= '
<div class="list-img col-sm-12 col-md-4 col-lg-3 col-xl-2 p-2">
<a href="/news/news-2589">
<img class="news-img" src="'.'" alt="直川科技入选闵行区科技小巨人工程! " width="100%" height="100%">
</a>
</div>
<div class="list-comt col-sm-12 col-md-8 col-lg-9 col-xl-10 p-2">
<div class="list-tit py-2">
<a href="'.$link.'">
<span>'. $title . '</span>
</a>
</div>
<div class="list-summary d-lg-block d-md-none d-sm-block py-2">
<span>直川科技入选闵行区科技小巨人工程!继获得上海市专精特新企业认定之后,直川科技又成功通过2022年度闵行区科技小巨人工程的认定!一直以来,直川...</span>
</div>
</div>';
$html .= "{$n}{$indent2}</li>{$n}";
}
$html12 = '<a class="more" href="support/productmanual/pushbutton/" target="_self"><span>查看详情</span> <i class="fa fa-chevron-right"></i></a>';
$html .= "{$indent2}$html12{$n}{$indent}</div></ul>{$n}";
}else{
//$html .= '<div class="">'.__('没有文章','b2').'</div>';
}
wp_reset_postdata();
return $html;
}
public function menu_2($item){
$t = "\t";
$n = "\n";
$indent = str_repeat( $t, 1 * 2 + $this->numtab() + 1 );
$indent2 = str_repeat( $t, 1 * 2 + $this->numtab() + 2 );
if(!isset($item->_menu_item_object)) return;
$type = $this->get_post_type($item->_menu_item_object);
$sub_menu_type = heku_get_menu_option( $item->ID , 'sub_menu_type' );
$args = array(
'post_type' => $type,
'posts_per_page' => 2,
'paged' => 0,
'no_found_rows' => true
);
if($type == 'post' && isset($item->object_id)){
$args['cat'] = $item->object_id;
}
$the_query = new \WP_Query( $args );
$html = '';
if($the_query->have_posts()){
$class_names = ' class="sub-menu sub-menu-1"';
$sss = ' class="container clearfix list-style01 py-3 '. $sub_menu_type . ' "';
$html .= "{$n}{$indent}<ul$class_names>";
$html .= "<div$sss>{$n}";
wp_reset_postdata();
while( $the_query->have_posts() ){
$the_query->the_post();
$link = get_permalink();
$title = get_the_title();
$imgs = get_post_meta( get_the_ID(), 'banner_fliter_group2', true );
$class_nameli = ' class="news-item row no-gutters align-items-center clear px-2 px-3 wow fadeInUp"';
//$thumb = b2_get_thumb(array('thumb'=>Post::get_post_thumb($the_query->post->ID),'type'=>'fill','width'=>$size['width'],'height'=>$size['height']));
$html .= "{$indent2}<li$class_nameli>";
$html .= '
<div class="list-img col-sm-12 col-md-4 col-lg-3 col-xl-2 p-2">
<a href="/news/news-2589">
<img class="news-img" src="'.'" alt="直川科技入选闵行区科技小巨人工程! " width="100%" height="100%">
</a>
</div>
<div class="list-comt col-sm-12 col-md-8 col-lg-9 col-xl-10 p-2">
<div class="list-tit py-2">
<a href="'.$link.'">
<span>'. $title . '</span>
</a>
</div>
<div class="list-summary d-lg-block d-md-none d-sm-block py-2">
<span>直川科技入选闵行区科技小巨人工程!继获得上海市专精特新企业认定之后,直川科技又成功通过2022年度闵行区科技小巨人工程的认定!一直以来,直川...</span>
</div>
</div>';
$html .= "{$n}{$indent2}</li>{$n}";
}
$html12 = '<a class="more" href="support/productmanual/pushbutton/" target="_self"><span>查看详情</span> <i class="fa fa-chevron-right"></i></a>';
$html .= "{$indent2}$html12{$n}{$indent}</div></ul>{$n}";
}else{
//$html .= '<div class="">'.__('没有文章','b2').'</div>';
}
wp_reset_postdata();
return $html;
}
public function menu_3($item){
$t = "\t";
$n = "\n";
$indent = str_repeat( $t, 1 * 2 + $this->numtab() + 1 );
if(!isset($item->_menu_item_object)) return;
$type = $this->get_post_type($item->_menu_item_object);
$sub_menu_type = heku_get_menu_option( $item->ID , 'sub_menu_type' );
$args = array(
'post_type' => $type,
'posts_per_page' => 2,
'paged' => 0,
'no_found_rows' => true
);
if($type == 'post' && isset($item->object_id)){
$args['cat'] = $item->object_id;
}
$the_query = new \WP_Query( $args );
$html = '';
if($the_query->have_posts()){
$class_names = ' class="sub-menu sub-menu-1"';
$sss = ' class="container clearfix list-style01 py-3 '. $sub_menu_type . ' "';
$html .= "{$n}{$indent}<ul$class_names>{$n}";
$html .= "{$indent}<div$sss>{$n}";
wp_reset_postdata();
while( $the_query->have_posts() ){
$the_query->the_post();
$link = get_permalink();
$title = get_the_title();
$imgs = get_post_meta( get_the_ID(), 'banner_fliter_group2', true );
$class_nameli = ' class="news-item row no-gutters align-items-center clear px-2 px-3 wow fadeInUp"';
//$thumb = b2_get_thumb(array('thumb'=>Post::get_post_thumb($the_query->post->ID),'type'=>'fill','width'=>$size['width'],'height'=>$size['height']));
$html .= "{$indent}<li$class_nameli>";
$html .= '
<div class="list-img col-sm-12 col-md-4 col-lg-3 col-xl-2 p-2">
<a href="/news/news-2589">
<img class="news-img" src="'.$imgs[0]['banner_primg1'].'" alt="直川科技入选闵行区科技小巨人工程! " width="100%" height="100%">
</a>
</div>
<div class="list-comt col-sm-12 col-md-8 col-lg-9 col-xl-10 p-2">
<div class="list-tit py-2">
<a href="'.$link.'">
<span>'. $title . '</span>
</a>
</div>
<div class="list-summary d-lg-block d-md-none d-sm-block py-2">
<span>直川科技入选闵行区科技小巨人工程!继获得上海市专精特新企业认定之后,直川科技又成功通过2022年度闵行区科技小巨人工程的认定!一直以来,直川...</span>
</div>
</div>';
$html .= "{$n}{$indent}</li>{$n}";
}
$html .= '<a class="more" href="support/productmanual/pushbutton/" target="_self" class="more"><span>查看详情</span> <i class="fa fa-chevron-right"></i></a></ul>';
}else{
//$html .= '<div class="">'.__('没有文章','b2').'</div>';
}
wp_reset_postdata();
return $html;
}
public function get_post_type($item_boject){
$type = 'post';
switch($item_boject){
case 'category';
$type = 'post';
break;
case 'shoptype';
$type = 'shop';
break;
case 'labtype';
$type = 'labs';
case 'mp';
$type = 'pps';
break;
}
return $type;
}
}