Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is your function man:</p> <pre><code>class Custom_Walker_Nav_Menu extends Walker_Nav_Menu { function start_el(&amp;$output, $item, $depth, $args) { global $wp_query; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $class_names = $value = ''; $classes = empty( $item-&gt;classes ) ? array() : (array) $item-&gt;classes; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); $class_names = ' class="'. esc_attr( $class_names ) . '"'; $output .= $indent . '&lt;li id="menu-item-'. $item-&gt;ID . '" ' . $value . $class_names .'&gt;'; $attributes = ! empty( $item-&gt;attr_title ) ? ' title="' . esc_attr( $item-&gt;attr_title ) .'"' : ''; $attributes .= ! empty( $item-&gt;target ) ? ' target="' . esc_attr( $item-&gt;target ) .'"' : ''; $attributes .= ! empty( $item-&gt;xfn ) ? ' rel="' . esc_attr( $item-&gt;xfn ) .'"' : ''; $attributes .= ! empty( $item-&gt;url ) ? ' href="' . esc_attr( $item-&gt;url ) .'"' : ''; $prepend = '&lt;strong&gt;'; $append = '&lt;/strong&gt;'; $description = '&lt;div id="querySort"&gt;&lt;/div&gt;'; $item_output = $args-&gt;before; $item_output .= '&lt;a'. $attributes .'&gt;'; $item_output .= $args-&gt;link_before .$prepend.apply_filters( 'the_title', $item-&gt;title, $item-&gt;ID ).$append; $item_output .= $description.$args-&gt;link_after; $item_output .= '&lt;/a&gt;'; $item_output .= $args-&gt;after; //ajout du nb de posts $submenus = $depth == 0 ? get_posts( array( 'post_type' =&gt; 'nav_menu_item', 'numberposts' =&gt; -1, 'orderby' =&gt; 'menu_order', 'order' =&gt; 'ASC', 'meta_query' =&gt; array( array( 'key' =&gt; '_menu_item_menu_item_parent', 'value' =&gt; $item-&gt;ID ) ) ) ) : false; $item_output .= $submenus ? ' &lt;span class="submenus-count"&gt;(' . count( $submenus ) . ')&lt;/span&gt;' : ''; $item_output .= $args-&gt;after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); //add last post for each subcategories if( $item-&gt;type == 'taxonomy' &amp;&amp; $item-&gt;post_parent != 0 ){ $list_recent_cat_post = '&lt;ul class="show-hide"&gt;'; $args = array( 'numberposts' =&gt; 4, 'category__in' =&gt; $item-&gt;object_id ); $myposts = get_posts( $args ); foreach( $myposts as $mypost ) : $list_recent_cat_post .= '&lt;li&gt;&lt;a href="' . get_permalink($mypost-&gt;ID) . '"&gt;' . $mypost-&gt;post_title . '&lt;/a&gt;&lt;/li&gt;'; endforeach; $list_recent_cat_post .= '&lt;/ul&gt;'; }else{ $list_recent_cat_post = ''; } $link = '&lt;a href="' . get_category_link( $category-&gt;term_id ) . '" '; if ( $use_desc_for_title == 0 || empty($category-&gt;description) ) $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"'; else $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category-&gt;description, $category ) ) ) . '"'; $link .= '&gt;'; $link .= $cat_name . '&lt;/a&gt;'; if ( (! empty($feed_image)) || (! empty($feed)) ) { $link .= ' '; if ( empty($feed_image) ) $link .= '('; $link .= '&lt;a href="' . get_category_feed_link($category-&gt;term_id, $feed_type) . '"'; if ( empty($feed) ) $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; else { $title = ' title="' . $feed . '"'; $alt = ' alt="' . $feed . '"'; $name = $feed; $link .= $title; } $link .= '&gt;'; if ( empty($feed_image) ) $link .= $name; else $link .= "&lt;img src='$feed_image'$alt$title" . ' /&gt;'; $link .= '&lt;/a&gt;'; if ( empty($feed_image) ) $link .= ')'; } if ( isset($show_count) &amp;&amp; $show_count ) $link .= ' (' . intval($category-&gt;count) . ')'; if ( isset($show_date) &amp;&amp; $show_date ) { $link .= ' ' . gmdate('Y-m-d', $category-&gt;last_update_timestamp); } $link .= $list_recent_cat_post; if ( isset($current_category) &amp;&amp; $current_category ) $_current_category = get_category( $current_category ); if ( 'list' == $args-&gt;style ) { $output .= "\t&lt;li"; $class = 'cat-item cat-item-'.$category-&gt;term_id; if ( isset($current_category) &amp;&amp; $current_category &amp;&amp; ($category-&gt;term_id == $current_category) ) $class .= ' current-cat'; elseif ( isset($_current_category) &amp;&amp; $_current_category &amp;&amp; ($category-&gt;term_id == $_current_category-&gt;parent) ) $class .= ' current-cat-parent'; $output .= ' class="'.$class.'"'; $output .= "&gt;$link\n"; } else { $output .= "\t$link\n"; } } } </code></pre> <p>The problem with your function was that you hoped that variable $category is set. This is an mistake. Another one was that you did not check for menu item type. You wanted to apply submenu only for menu item of taxonomy type. And to get posts from that taxonomy, you have to get posts with category__in arg containing category ID, which you can get from $item->object_id.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload