{"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

\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 . '
  • ID . '\"' .\n $value . $class_names .'>';\n\n $attributes = ! empty( $item->attr_title ) ? ' title=\"' .\n esc_attr( $item->attr_title ) .'\"' : '';\n $attributes .= ! empty( $item->target ) ? ' target=\"' .\n esc_attr( $item->target ) .'\"' : '';\n $attributes .= ! empty( $item->xfn ) ? ' rel=\"' .\n esc_attr( $item->xfn ) .'\"' : '';\n $attributes .= ! empty( $item->url ) ? ' href=\"' .\n esc_attr( $item->url ) .'\"' : '';\n\n $item_output = $args->before;\n $item_output .= '';\n $item_output .= $args->link_before .\n apply_filters( 'the_title', $item->title, $item->ID )\n . $args->link_after;\n $item_output .= '
    ' . $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

    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

    \n$walker = new Menu_With_Description;\n<\/pre>\n

    a samotnou funkci wp_nav_menu dopln\u00edme o ‚walker‘ => $walker, nap\u0159\u00edklad:<\/p>\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

    Dne\u0161n\u00ed n\u00e1vod je u konce, pokud si nejste jisti, jak pou\u017e\u00edt funkci wp_nav_menu, pod\u00edvejte se pros\u00edm do codexu, nebo v\u00e1s odk\u00e1\u017ei na star\u0161\u00ed \u010dl\u00e1nek Jak vytvo\u0159it editovateln\u00e9 menu<\/a>.<\/p>\n","protected":false},"excerpt":{"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","protected":false},"author":1,"featured_media":1117,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[410,562],"class_list":["post-1115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","tag-walker","tag-zobrazeni-description-v-menu"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1115"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=1115"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/1117"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}