{"id":1115,"date":"2013-09-16T18:35:41","date_gmt":"2013-09-16T18:35:41","guid":{"rendered":"http:\/\/musilda.cz\/?p=1115"},"modified":"2013-09-16T18:35:41","modified_gmt":"2013-09-16T18:35:41","slug":"zobrazeni-menu-description-ve-wordpress-sablone","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/zobrazeni-menu-description-ve-wordpress-sablone\/","title":{"rendered":"Zobrazen\u00ed menu description ve WordPress \u0161ablon\u011b"},"content":{"rendered":"
Pln\u011b editovateln\u00e9 menu je ve WordPressu ji\u017e n\u011bjakou dobu a pomoc\u00ed funkce wp_nav_menu m\u016f\u017eete zm\u011bnit a upravit ka\u017edou \u010d\u00e1st generovan\u00e9ho k\u00f3du. Z\u00e1rove\u0148 je k dispozici skryt\u00e1 nab\u00eddka pro zobrazen\u00ed dal\u0161\u00edch informac\u00ed, v\u010detn\u011b popisu odkazu. A zobrazen\u00edm tohoto odkazu se budeme dnes zab\u00fdvat.<\/p>\n
P\u0159i z\u00e1kladn\u00edm nastaven\u00ed m\u00e1te v nab\u00eddce u odkazu pouze odkaz, text odkazu a titulek. Jak ukazuj\u00ed obr\u00e1zky, po za\u0161krtnut\u00ed dal\u0161\u00edch mo\u017enost\u00ed se zobraz\u00ed pole pro p\u0159id\u00e1n\u00ed css t\u0159\u00eddy k odkazu, vztah odkazu, target a popis.<\/p>\n
<\/a><\/p>\n <\/a><\/p>\n Na n\u011bkter\u00fdch WordPress \u0161ablon\u00e1ch m\u016f\u017eete vid\u011bt v menu, pod n\u00e1zvem odkazu i dal\u0161\u00ed text, jako nap\u0159\u00edklad na poveden\u00e9 Corrito<\/a> od Themes4all. Samoz\u0159ejm\u011b to nen\u00ed probl\u00e9m nak\u00f3dovat v html, ale my m\u00e1me redak\u010dn\u00ed syst\u00e9m a chceme d\u00e1t u\u017eivateli mo\u017enost si popis upravit.<\/p>\n Funkce wp_nav_menu obsahuje parametr „walker“. Tento parametr ur\u010duje vlastn\u00ed walker objekt. Deafultn\u011b to je Walker_Nav_Menu. Pomoc\u00ed vlastn\u00ed t\u0159\u00eddy uprav\u00edme walkera a p\u0159iprav\u00edme menu na zobrazov\u00e1n\u00ed popisu.<\/p>\n Nebudu v\u00e1s zdr\u017eovat a rovnou do functions.php vlo\u017e\u00edme n\u00e1sleduj\u00edc\u00ed k\u00f3d:<\/p>\n V podstat\u011b upravujeme funkci start_el, kter\u00e1 je sou\u010d\u00e1st\u00ed p\u016fvodn\u00ed t\u0159\u00eddy. D\u016fle\u017eit\u00e1 je pro n\u00e1s \u010d\u00e1st $item_output, kde m\u016f\u017eete vid\u011bt k\u00f3d odkazu a ‚ . $item->description . ‚<\/span>, co\u017e je samotn\u00fd v\u00fdpis popisu.<\/p>\n Dal\u0161\u00ed krok, kter\u00fd mus\u00edme ud\u011blat, je \u00faprava funkce wp_nav_menu. P\u0159ed k\u00f3d, ve kter\u00e9m vypisujeme menu v \u0161ablon\u011b, vlo\u017e\u00edme \u0159\u00e1dek s:<\/p>\n a samotnou funkci wp_nav_menu dopln\u00edme o ‚walker‘ => $walker, nap\u0159\u00edklad:<\/p>\n\nclass Menu_With_Description extends Walker_Nav_Menu {\n function start_el(&$output, $item, $depth, $args) {\n global $wp_query;\n $indent = ( $depth ) ? str_repeat( \"\\t\", $depth ) : '';\n\n $class_names = $value = '';\n\n $classes = empty( $item->classes ) ? array() : (array) $item->classes;\n\n $class_names = join( ' ', apply_filters( 'nav_menu_css_class',\n array_filter( $classes ), $item ) );\n $class_names = ' class=\"' . esc_attr( $class_names ) . '\"';\n\n $output .= $indent . '
' . $item->description . '<\/span>';\n $item_output .= '<\/a>';\n $item_output .= $args->after;\n\n $output .= apply_filters( 'walker_nav_menu_start_el',\n $item_output, $item, $depth, $args );\n }\n}\n\n<\/pre>\n\n$walker = new Menu_With_Description;\n<\/pre>\n
\nif ( has_nav_menu( 'primary-menu' ) ) {\n wp_nav_menu( array(\n 'theme_location' => 'primary-menu',\n 'menu_class' => 'sf-menu',\n 'walker' => $walker,\n 'container' => ''\n) );\n }\n<\/pre>\n