Chèn link vào widget menu wordpress

admin21/01/2024 278

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
admin05/05/2024 133
admin05/05/2024 85
admin30/04/2024 126
admin26/03/2024 201
admin09/03/2024 569
admin20/02/2024 267
admin19/02/2024 657
admin18/02/2024 570
Chia sẻ mới nhất
admin09/03/2024 569
admin20/02/2024 267
admin19/02/2024 657
admin18/02/2024 570
admin01/02/2024 333
admin31/01/2024 274
admin21/01/2024 278
admin21/01/2024 333
admin18/01/2024 330
admin16/01/2024 270