Wordpress custom sitemap shortcode following custom menu order


/ Published in: PHP
Save to your folder(s)

Use a shortcode [sitemap] to render a sitemap ordered by custom menu order (Wordpress 3+).


Copy this code and paste it in your HTML
  1. //Our custom sitemap which reads the menu order using extended menu walker class
  2. //Shortcode
  3. function wdp_shortcode_sitemap($atts){ //[sitemap]
  4. extract(shortcode_atts(array(
  5. ), $atts));
  6. //call the function
  7. $shortcodecontent = wdp_sitemap();
  8.  
  9. return $shortcodecontent;
  10. }
  11. add_shortcode('sitemap', 'wdp_shortcode_sitemap');
  12.  
  13. //call the menu and use our custom walker
  14. function wdp_sitemap(){
  15. return wp_nav_menu(array('theme_location' => 'primary', 'walker' => new wdp_sitemap_walker(), 'fallback_cb' => '', 'echo'=>false));
  16. }
  17.  
  18. //extended navigation class for sitemap rendering
  19. class wdp_sitemap_walker extends Walker_Nav_Menu
  20. {
  21. function start_el(&$output, $item, $depth, $args)
  22. {
  23. global $wp_query;
  24. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  25. $id=strtolower(str_replace(" ","-",$item->title));
  26. $id=str_replace(array('&','&'),"",$id);
  27. $id=str_replace('--',"-",$id);
  28. $output .= $indent . '<li id="sitemap-'. $id . '"' . $value .'>';
  29. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  30. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  31. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  32. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  33.  
  34. //$item_output .= $args->before;
  35. $item_output .= '<a'. $attributes .'>';
  36. $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
  37. // $item_output .= $args->link_after;
  38. $item_output .= '</a>';
  39. //$item_output .= $args->after;
  40. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.