Note that there are some explanatory texts on larger screens.

plurals
  1. POShow latest posts for each subcategory in a menu?
    primarykey
    data
    text
    <p>I'm trying to do a menu made of categories and subcategories. For each subcategory, I would like to display the latest posts in this category. I have tried to create a walker with bits of code found here and there, but right now it's displaying the same latest post for each item, and it's placed between the parent categorie and its children. How could I fetch the good category, and show the latest post only for the subcategories? Here is my code:</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 $cat_name = esc_attr( $category-&gt;name); $cat_name = apply_filters( 'list_cats', $cat_name, $category ); $list_recent_cat_post = '&lt;ul class="show-hide"&gt;'; $args = array( 'numberposts' =&gt; 4, 'category_name' =&gt; $category-&gt;name ); $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;'; $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['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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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