Skip to content

Instantly share code, notes, and snippets.

@darkiop
Created May 4, 2019 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkiop/ed818e7ca4dbaf1bc540544a76e4118b to your computer and use it in GitHub Desktop.
Save darkiop/ed818e7ca4dbaf1bc540544a76e4118b to your computer and use it in GitHub Desktop.
<?php
/**
* http://codex.wordpress.org/Function_Reference/wp_register_sidebar_widget
*/
class widget_submenu extends WP_Widget {
function __construct() {
parent::__construct(
'widget_submenu', // Base ID
__( 'Submenu', 'steinweiler.eu-2015' ), // Name
array( 'description' => __( 'submenu', 'steinweiler.eu-2015' ), ) // Args
);
}
// Formular für die Widget-Optionen
public function form( $instance ) {
$defaults = array(
'title' => __('Untermenü', 'steinweiler.eu-2015'),
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = esc_attr($instance['title']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Titel</label>
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
</p>
<?php
}
// Speichert die Widget-Optionen in der Datenbank
public function update( $new_instance, $old_instance ) {
$instance['title'] = strip_tags($new_instance['title']);
return $new_instance;
}
// Ausgabe des Widgets
public function widget( $args, $instance ) {
extract( $args );
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . $icon . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
echo $args['before_widget'];
/* Untermenü Seitenleiste
* http://codex.wordpress.org/Function_Reference/wp_list_pages#List_whole_subpages
*/
if (!is_search() && !is_404()) :
if (!$post->post_parent) {
$ancestor_title = get_post($post);
$ancestor_title = $ancestor_title->post_title;
if (DEBUG_STW == 1) :
debugVar($ancestor_title, '$ancestor_title');
endif;
}
if ($post->ancestors) {
$ancestor_title = $post->ancestors;
$ancestor_title = end($ancestor_title);
$ancestor_title = get_the_title( $ancestor_title );
if (DEBUG_STW == 1) :
debugVar($ancestor_title, '$ancestor_title');
endif;
}
if (!$post->post_parent){
// will display the subpages of this top level page
$args = array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0,
'sort_column' => 'post_title'
);
$children = wp_list_pages($args);
debugVar($children);
} else {
$ancestors = get_post_ancestors( $post->ID );
debugVar($ancestors);
#$ancestors2 = $post->ancestors;
#debugVar($ancestors2);
$ancestors = end($ancestors);
$args = array(
'title_li' => '',
'child_of' => $ancestors,
'echo' => 0,
'sort_column' => 'post_title'
);
$children = wp_list_pages($args);
#debugVar($children);
}
#debugVar($ancestor_title, "ancestor_title");
switch ($ancestor_title) {
case ("Startseite"):
$icon = "home";
break;
case ("Steinweiler"):
$icon = "star";
break;
case ("Leben im Dorf"):
$icon = "user";
break;
case ("Veranstaltungen"):
$icon = "calendar";
break;
case ("Gewerbe"):
$icon = "road";
break;
case ("Verwaltung"):
$icon = "cog";
break;
case ("Links"):
$icon = "link";
break;
case ("archiv"):
$icon = "search";
break;
default:
$icon = "star";
break;
}
$nav_title = $ancestor_title;
$nav_title = strtolower($nav_title);
$nav_title = str_replace(' ', '-', $nav_title);
#debugVar($nav_title);
if ($children) { ?>
<div class="widget nav-sidebar">
<div class="widget-header">
<h3><span class="glyphicon glyphicon-<?php echo $icon; ?>" aria-hidden="true"></span> <?php echo $ancestor_title; ?></h3>
</div>
<div class="widget-content">
<ul class="list-group">
<?php echo $children; ?>
</ul>
</div>
</div> <!-- /widget -->
<?php
}
endif;
echo $args['after_widget'];
}
} // /.class widget_submenu
register_widget( 'widget_submenu' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment