1

How to add recents posts to the menu? Entries with pictures. "Menu-item-object-custom" items in a div container?

What I have now is: header.php

wp_nav_menu(
    array(
        'theme_location' => 'menu-1',
        'menu_id'        => 'primary-menu',
        'menu_class'     => 'display_lasts_ten_posts_for_categories_menu_item',
        'walker'         => new WPSE_78121_Sublevel_Walker,
        )
    );

functions.php

add_filter('wp_get_nav_menu_items','display_lasts_ten_posts_for_categories_menu_item',10,3);
function display_lasts_ten_posts_for_categories_menu_item( $items, $menu, $args ) {

$menu_order = count($items);
$child_items = array();

foreach ( $items as $item ) {
    if ( 'category' != $item->object || ('category' == $item->object && get_category_children($item->object_id)) )
        continue;
    $category_ten_last_posts = array(
        'numberposts' => 3,
        'cat' => $item->object_id,
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_query'     => array(
            array(
                'key'     => 'wazny',
                'value'   => 0,
                'compare' => 'LIKE',
            ),
        ),
    );
    foreach ( get_posts( $category_ten_last_posts ) as $post ) {

        $post->menu_item_parent = $item->ID;
        $post->post_type = 'nav_menu_item';
        $post->object = 'custom';
        $post->type = 'custom';
        $post->menu_order = ++$menu_order;
        $post->title = $post->post_title;
        $post->url = get_permalink( $post->ID );
        $child_items[]= $post;

    }

}
return array_merge( $items, $child_items );
}


class WPSE_78121_Sublevel_Walker extends Walker_Nav_Menu
{
function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class='sub-menu'><div class='sub-menu-wrap'>\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "$indent</div></ul>\n";
}
}

I want to achieve this effect: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.