Home › Forum › Problemi Vari con WP › Menu personalizzato in Sidebar
-
AutorePost
-
-
29 Aprile 2013 alle 9:08 #24461galbu89Partecipante
Ciao,
ho un problema legato alla visualizzazione di un menu personalizzato nella sidebar del mio template:
ho creato un menu dal back-end e questo lo voglio mostrare nella sidebar con nome e composizione delle pagine elencate rispettando il grado di parentela, come qui sotto mostrato
Al momento, ho questo codice che mi permette di visualizzare il menu nella mie pagine, ma senza rispettare il rapporto parent-child e senza riportare il titolo che dò al mio menu.
Riuscite a darmi una mano, grazie.
$meta_box_menu = array(
'id' => 'custom-meta-menu',
'title' => 'Menu Sidebar',
'page' => 'page',
'context' => 'side',
'priority' => 'high',
'fields' => array(
array(
'id' => 'custom-meta-menu-name',
'type' => 'select',
'std' => 'none'
),
),
);
/*
* This function will register the meta box with WordPress
*/
function custom_add_box() {
global $meta_box_menu;
add_meta_box($meta_box_menu, $meta_box_menu, 'custom_meta_menu_html', $meta_box_menu, $meta_box_menu, $meta_box_menu);
}
add_action('admin_init', 'custom_add_box');
/*
* This function will produce the html needed to display our meta box in the admin area
*/
function custom_meta_menu_html() {
global $meta_box_menu, $post;
$output = '<p style="padding:10px 0 0 0;">'.__('Scegli il menu da mostrare nella Sidebar di questa pagina.', 'custom').'</p>';
$output .= '<input type="hidden" name="sf_meta_box_nonce" value="'. wp_create_nonce(basename(__FILE__)). '" />';
$output .= '<table class="form-table">';
foreach ($meta_box_menu as $field) {
$meta = get_post_meta($post->ID, $field, true);
/*
* Get out all our menus using the function from functions.php
*/
$menus = custom_get_all_menus();
/*
* Grab out saved data for edit mode
*/
$meta = get_post_meta($post->ID, $field, true);
$output .= '<select name="'.$field.'" class="widefat">';
$output .= '<option value="none">- none -</option>';
if(is_array($menus)):
foreach($menus as $k => $v):
if($meta==$v->slug):
$output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
else:
$output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
endif;
endforeach;
endif;
$output .= '</select>';
}
$output .= '</table>';
echo $output;
}
/*
* This function will save our preferences into the database
*/
function custom_save_data($post_id) {
global $meta_box, $meta_box_menu;
foreach ($meta_box_menu as $field) {
$old = get_post_meta($post_id, $field, true);
$new = $_POST[$field];
if ($new && $new != $old) {
update_post_meta($post_id, $field, stripslashes(htmlspecialchars($new)));
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field, $old);
}
}
}
add_action('save_post', 'custom_save_data');
function custom_get_all_menus() {
$menu_obj = get_terms( 'nav_menu' );
return $menu_obj;
}
/*
* Html Custom Sidebar Menu
*/
function custom_generate_menu_from_items($items) {
if(count($items)>0):
$menu_list = '<aside class="widget sidebar_menu"><h5 class="widget-title">' .'CUSTOM MENU'. '</h5><nav><ul class="sidebar-menu-items">';
foreach ( (array) $items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '
- ' . $title . '
';
}
$menu_list .= '</nav></aside>';
return $menu_list;
$menu_item->title = get_post_meta( $menu_item->ID, '_menu_item_title', true );
return $menu_item;
endif;
add_filter( 'wp_get_nav_menu_items', 'custom_generate_menu_from_items', null, 3 );
}
-
29 Aprile 2013 alle 14:24 #103280elmaniseroPartecipante
Utilizzare il widget menù personalizzato.
-
29 Aprile 2013 alle 16:07 #103284galbu89Partecipante
Grazie elmanisero, ma con il widget menu personalizzato, non ho una gestione flessibile per l’applicazione di un menu diverso per ciascuna pagina. Perchè vorrebbe dire creare una pagina-template per ciascuna pagina.
Invece, con questo codice, posso selezionare il menu da applicare alla sidebar di ciascuna pagina, utilizzando anche un solo template-pagina.
-
29 Aprile 2013 alle 16:55 #103285
-
30 Aprile 2013 alle 7:10 #103291galbu89Partecipante
Grazie ancora elmanisero.
Ma il problema non è la possibilità di scegliere un widget per ciascun sidebar, perchè questo sono riuscito a farlo con il codice inserito.
Il problema è visualizzare il menu con le relazioni parent-child, che devono essere mantenute cosଠcome impostate nella gerarchia del menu.
-
30 Aprile 2013 alle 7:42 #103292elmaniseroPartecipante
Il widget menu personalizzato ti mantiene la gerarchia.
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.