我試圖創建具有兩列(即,其中我可以選擇兩個不同的菜單,並讓它們在單個顯示並排側自定義菜單窗口小部件側邊欄部件)。WordPress的自定義菜單的widget ==>變化到2列
我發現tutorial關於如何創建一個雙列文本小部件,但我不知道如何編輯自定義菜單小部件函數來添加和實現一個額外的菜單 - 我試圖創建一個額外的菜單變量(即,「$ menus1」和「$ menus2」)破解WordPress(「意外的T字符串」錯誤)。
,那麼「$實例」陣列有一個名爲「nav_menu」一個預先定義的鍵或此鍵,它的值由「形式」功能創建?如果是後者,我可以定義'nav_menu1'和'nav_menu2'鍵(我之前嘗試過,但也收到了「意外的T字符串」錯誤)?
任何幫助,將不勝感激。
<?php
/**
* Custom Menu widget copied from default WP Widget Class and adjusted for 2-columns
*/
class SO_ABC_Tag_List_Widget_2col extends WP_Widget {
function __construct() {
$widget_ops = array('description' => __('By adding this widget you can select your alphabetical tags menu to display on your website.'));
parent::__construct('so_abc_tags_2col', __('SO ABC Tag List 2 col'), $widget_ops);
}
function widget($args, $instance) {
// Get menu
$nav_menu1 = ! empty($instance['nav_menu']) ? wp_get_nav_menu_object($instance['nav_menu']) : false;
$nav_menu2 = ! empty($instance['nav_menu']) ? wp_get_nav_menu_object($instance['nav_menu']) : false;
if (!$nav_menu)
return;
$instance['title'] = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
echo $args['before_widget'];
if (!empty($instance['title']))
echo $args['before_title'] . '<i class="icon-tags"></i> ' . $instance['title'] . $args['after_title'];
<div style="float: left; width: 45%">
wp_nav_menu(array('fallback_cb' => '', 'menu' => $nav_menu1));
</div>
<div style="float: left; width: 45%"></div>
wp_nav_menu(array('fallback_cb' => '', 'menu' => $nav_menu2));
</div>
echo $args['after_widget'];
}
function update($new_instance, $old_instance) {
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
$instance['nav_menu'] = (int) $new_instance['nav_menu'];
$instance['nav_menu'] = (int) $new_instance['nav_menu'];
return $instance;
}
function form($instance) {
$title = isset($instance['title']) ? $instance['title'] : '';
$nav_menu1 = isset($instance['nav_menu']) ? $instance['nav_menu'] : '';
$nav_menu2 = isset($instance['nav_menu']) ? $instance['nav_menu'] : '';
// Get menus
$menus1 = get_terms('nav_menu', array('hide_empty' => false));
$menus1 = get_terms('nav_menu', array('hide_empty' => false));
// If no menus exists, direct the user to go and create some.
if ((!$menus1 ) AND (!menus2)) {
echo '<p>'. sprintf(__('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php')) .'</p>';
return;
}
?>
<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 $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'); ?>">
<?php
foreach ($menus1 as $menu1) {
echo '<option value="' . $menu1->term_id . '"'
. selected($nav_menu, $menu1->term_id, false)
. '>'. $menu1->name . '</option>';
}
?>
</select>
</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'); ?>">
<?php
foreach ($menus2 as $menu2) {
echo '<option value="' . $menu2->term_id . '"'
. selected($nav_menu, $menu2->term_id, false)
. '>'. $menu2->name . '</option>';
}
?>
</select>
</p>
<?php
}
}