PHP 32
ACF menu walker By micha on 12th May 2020 04:28:37 PM
  1. // replace original walker `$item_output`
  2.  
  3.     /*
  4.      * Anchor Nav
  5.      */
  6.     if ( get_field('anchor_link', $item->ID) ) {
  7.       if ( empty(get_field('anchor_target', $item->ID)) ) {
  8.         $slug = $slug;
  9.       } else {
  10.         $slug = get_field('anchor_target', $item->ID);
  11.       }
  12.       $attributes  = !empty( $item->attr_title ) ? ' title="'.esc_attr( $item->attr_title ).'"' : '';
  13.       $attributes .= !empty( $item->target )? ' target="'.esc_attr( $item->target ).'"' : '';
  14.       $attributes .= !empty( $item->xfn ) ? ' rel="'. esc_attr( $item->xfn ).'"' : '';
  15.       if (is_front_page()) {
  16.         $attributes .= !empty( $item->url ) ? ' href="#'.esc_attr( $slug ).'"' : '';
  17.       } else {
  18.         $attributes .= !empty( $item->url ) ? ' href="'.esc_attr( get_bloginfo('url') ).'/#'.$slug.'"' : '';
  19.       }
  20.  
  21.       $output .= '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . ' anchor-link">';
  22.  
  23.     } else {
  24.  
  25.       $attributes  = !empty( $item->attr_title ) ? ' title="'.esc_attr( $item->attr_title ).'"' : '';
  26.       $attributes .= !empty( $item->target )? ' target="'.esc_attr( $item->target ).'"' : '';
  27.       $attributes .= !empty( $item->xfn ) ? ' rel="'. esc_attr( $item->xfn ).'"' : '';
  28.       $attributes .= !empty( $item->url ) ? ' href="'.esc_attr( $item->url ).'"' : '';
  29.  
  30.       $output .= '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
  31.  
  32.     }
  33.  
  34.  
  35.     $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
  36.       $args->before,
  37.       $attributes,
  38.       $args->link_before,
  39.       apply_filters( 'the_title', $item->title, $item->ID ),
  40.       $args->link_after,
  41.       $args->after
  42.     );
  43.     // build html
  44.     $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.