Chèn link vào widget menu wordpress

admin21/01/2024 66

Bạn dùng widget menu để hiện thị menu ra siderbar tuy nhiên khi click vào tiêu đề của widget thì không ra được link. Hãy dùng đoạn code sau để overide widget mặc định của menu wordpress nhé.

if (!class_exists('Abweb_WP_Nav_Menu_Widget')) {
class Abweb_WP_Nav_Menu_Widget extends WP_Nav_Menu_Widget {

public function widget($args, $instance) {
echo $args['before_widget'];

if (!empty($instance['title'])) {
echo $args['before_title'] . '<a href="' . esc_url($instance['link']) . '">' . apply_filters('widget_title', $instance['title'], $instance, $this->id_base) . '</a>' . $args['after_title'];
}
if (!empty($instance['nav_menu'])) {
echo '<div class="menu-container">';
$nav_menu_args = array('menu' => $instance['nav_menu']);
wp_nav_menu(apply_filters('widget_nav_menu_args', $nav_menu_args, $instance, $this->id_base));
echo '</div>';
}

echo $args['after_widget'];
}

public function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$nav_menu = isset($instance['nav_menu']) ? esc_attr($instance['nav_menu']) : '';
$link = isset($instance['link']) ? esc_url($instance['link']) : '#';
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
<?php
echo '<select id="' . $this->get_field_id('nav_menu') . '" name="' . $this->get_field_name('nav_menu') . '" class="widefat">';
echo '<option value="">'. esc_html__('— Select —', 'text_domain') .'</option>';
foreach (wp_get_nav_menus() as $menu) {
echo '<option value="' . $menu->term_id . '" ' . selected($nav_menu, $menu->term_id, false) . '>' . esc_html($menu->name) . '</option>';
}
echo '</select>';
?>
</p>

<p>
<label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link URL:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>" />
</p>
<?php
}

public function update($new_instance, $old_instance) {
$instance = parent::update($new_instance, $old_instance);
$instance['link'] = esc_url($new_instance['link']);

return $instance;
}
}
}

function replace_Abweb_wp_nav_menu_widget() {
unregister_widget('WP_Nav_Menu_Widget');
register_widget('Abweb_WP_Nav_Menu_Widget');
}

add_action('widgets_init', 'replace_Abweb_wp_nav_menu_widget');
Hỏi đáp

Bài viết mới nhất
admin20/02/2024 30
admin19/02/2024 285
admin18/02/2024 266
admin01/02/2024 60
admin31/01/2024 57
admin28/01/2024 72
admin21/01/2024 64
admin21/01/2024 66
Chia sẻ mới nhất
admin20/02/2024 30
admin19/02/2024 285
admin18/02/2024 266
admin01/02/2024 60
admin31/01/2024 57
admin21/01/2024 66
admin21/01/2024 64
admin18/01/2024 97
admin16/01/2024 77
admin02/01/2024 96