Chèn link vào widget menu wordpress

admin21/01/2024 2307

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
admin19/12/2024 25
admin19/12/2024 19
admin19/12/2024 18
admin19/12/2024 24
admin09/12/2024 49
_load_textdomain_just_in_time
admin21/11/2024 122
admin21/11/2024 99
admin20/11/2024 265
Chia sẻ mới nhất
admin09/12/2024 49
_load_textdomain_just_in_time
admin21/11/2024 122
admin21/11/2024 99
admin20/11/2024 265
admin09/03/2024 2170
admin20/02/2024 916
admin19/02/2024 1604
admin18/02/2024 1500
admin01/02/2024 977
admin31/01/2024 937