Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Created July 29, 2018 21:11
Show Gist options
  • Save eto4detak/52ccb81abf952ce7ea3b3a8de571e8ae to your computer and use it in GitHub Desktop.
Save eto4detak/52ccb81abf952ce7ea3b3a8de571e8ae to your computer and use it in GitHub Desktop.
wp php widget menu custom
<?php
class Asc_ch_WP_Nav_Menu_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'col masonry-col', //задать клаасс CSS
'description' => __( 'Add a custom menu to your sidebar +' ),
'customize_selective_refresh' => true,
);
parent::__construct( 'ask_widget_nav_menu', __('Меню +'), $widget_ops );
}
public function widget( $args, $instance ) {
// Get menu
$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
if ( !$nav_menu )
return;
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$menu = wp_get_nav_menu_object($instance['nav_menu']); // получаем объект меню
$menu_items = wp_get_nav_menu_items( $menu ); // получаем элементы меню
echo $args['before_widget'];
echo '<div class="sidebar-item"><div class="blogs-tags clearfix blogs-tags-notitle">';
if ( $title ) { echo $args['before_title'] . 'Блоги из рубрик' . $args['after_title'];}
echo "<ul class='question-tags-in'>";
foreach( $menu_items as $item ){
?>
<li class="rubrik <?php if(0) echo 'rubrik-active'; ?>"><a href="<?php esc_html_e($item->url); ?>"><?php esc_html_e($item->title) ?></a></li>
<?php
}
echo "</ul>";
?>
<div class="collapsed-tags-more">
<a href="#" data-class="cp-tags-list" data-show="↓&nbsp;&nbsp;&nbsp;Показать все рубрики" data-hide="↑&nbsp;&nbsp;&nbsp;Скрыть рубрики" class="collapsed-plugin__btn"><span>↓&nbsp;&nbsp;&nbsp;Показать все рубрики</span></a>
</div>
<?php
echo '</div></div>';
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = array();
if ( ! empty( $new_instance['title'] ) ) {
$instance['title'] = sanitize_text_field( $new_instance['title'] );
}
if ( ! empty( $new_instance['nav_menu'] ) ) {
$instance['nav_menu'] = (int) $new_instance['nav_menu'];
}
return $instance;
}
public function form( $instance ) {
global $wp_customize;
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
// Get menus
$menus = wp_get_nav_menus();
// If no menus exists, direct the user to go and create some.
?>
<p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
<?php
if ( $wp_customize instanceof WP_Customize_Manager ) {
$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
} else {
$url = admin_url( 'nav-menus.php' );
}
?>
<?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
</p>
<div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
<option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
<?php foreach ( $menus as $menu ) : ?>
<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
<?php echo esc_html( $menu->name ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
<p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>">
<button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button>
</p>
<?php endif; ?>
</div>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment