Advertisement
ritti1989

Eigenes Widget inkl. Walkeranpassung

Jul 27th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /* Register Navigation */
  2. function register_my_menus() {
  3. register_nav_menus(
  4. array(
  5. 'cat_nav' => __( 'Katzen-Navigation'),
  6. )
  7. );
  8. }
  9. add_action ('init', 'register_my_menus');
  10.  
  11. /* Widget: Dein eigenes Widget */
  12. class Toni_Menu_Widget extends WP_Widget {
  13. function Toni_Menu_Widget() {
  14. $widget_ops = array('classname' => 'widget_wp_own_nav', 'description' => 'Dieses Widget zeigt die tolle Katzen-Navigation an..' );
  15. $this->WP_Widget('wp_own_nav', 'Rassekatzen » Katzen Menรผ', $widget_ops);
  16. }
  17.  
  18. function widget($args, $instance) {
  19. extract($args, EXTR_SKIP);
  20. global $post;
  21.  
  22. echo $before_widget;
  23.  
  24. if($instance['title']) {?><p class="h1"><?php echo $instance['title'] ?></p><?php }
  25.  
  26. wp_nav_menu(
  27. array(
  28. 'menu' => 'cat_navigtion', /* menu name */
  29. 'menu_class' => 'blognavigation',
  30. 'theme_location' => 'cat_nav', /* where in the theme it's assigned */
  31. 'container' => 'false', /* container class */
  32. 'depth' => '1', /* suppress lower levels for now */
  33. 'walker' => new description_walker()
  34. )
  35. );
  36.  
  37. echo $after_widget;
  38. }
  39.  
  40. function update($new_instance, $old_instance) {
  41. $instance = $old_instance;
  42. $instance['title'] = strip_tags($new_instance['title']);
  43.  
  44. return $instance;
  45. }
  46.  
  47. function form($instance) {
  48. $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
  49. $title = $instance['title'];
  50. }
  51. }
  52. register_widget('Toni_Menu_Widget');
  53.  
  54. /* Walker */
  55. class description_walker extends Walker_Nav_Menu {
  56. function start_el(&$output, $item, $depth, $args) {
  57. global $wp_query;
  58. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  59.  
  60. $class_names = $value = '';
  61.  
  62. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  63.  
  64. $class_names .= join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  65. $class_names = ' class="'. esc_attr( $class_names ) . '"';
  66.  
  67. if(apply_filters( 'the_title', $item->title, $item->ID ) != "divider") {
  68. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  69. }
  70.  
  71. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  72. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  73. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  74. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  75.  
  76. $item_output = $args->before;
  77. $item_output .= '<a'. $attributes .'>';
  78.  
  79. /* Hier das ACF Field einbauen */
  80. if(get_field('name_des_fields', 'category_'.$item->ID') {
  81. $item_output .= get_field('name_des_fields', 'category_'.$item->ID');
  82. }
  83.  
  84. $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
  85. $item_output .= $args->link_after;
  86. $item_output .= '</a>';
  87. $item_output .= $args->after;
  88. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  89. }
  90.  
  91. function start_lvl(&$output, $depth) {
  92. $indent = str_repeat("\t", $depth);
  93. $output .= "\n$indent<ul>\n";
  94. }
  95.  
  96. function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  97. $id_field = $this->db_fields['id'];
  98. if ( is_object( $args[0] ) ) {
  99. $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
  100. }
  101.  
  102. return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement