Drop Down Navigation Menu That Shows Latest Post per Category

So I’m trying to work on my navigation menu in my wordpress website. I’m trying to copy the navigation menu from hongkiat.com (shown in the image).

In Hongkiat’s navigation menu, you’ll see the FIVE (5) Parent Categories (Design /dev, Technology, Inspiration, SOcial COmmerce and Deal). Once the user hovered on a Parent Category, it will show the recent posts under the Parent Category.

Read More

Anyway, I managed to code the Drop down menu with the Parent Category showing plus it’s child category. Now the dilemma lies on how can I display the latest post (at least 3 posts) under the parent category that is hovered by the user.

enter image description here

Anyway, here’s my code:

HTML/PHP

<ul>
    <?php 

        global $post;
        $myposts = get_posts('numberposts=3&offset=1');
        foreach($myposts as $post) ;


        $args = array(
        'show_option_all'    => '',
        'hide_empty'         => '0',
        'orderby'            => 'name',
        'order'              => 'ASC',
        'style'              => 'list',
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'hierarchical'       => 1,
        'title_li'           => (''),
        'show_option_none'   => __( '' ),
        'number'             => null,
        'echo'               => 1,
        'depth'              => 2,
        'current_category'   => 0,
        'pad_counts'         => 0,
        'taxonomy'           => 'category',
        'walker'             => null
        );
        wp_list_categories( $args ); 
    ?>
</ul>

CSS

.navone {
    display:inline-block;
    height:35px;
    vertical-align:middle;
    margin:0px auto;
    font-family: "Raleway",san-serif;
    font-feature-settings: normal;
    font-kerning: auto;
    font-language-override: normal;
    font-size: 12px;
    font-size-adjust: none;
    font-stretch: normal;
    font-style: normal;
    font-synthesis: weight style;
    font-variant: normal;
    font-weight: 600 !important;
    letter-spacing: 0.03em;
    text-transform:uppercase;
}

.navone ul {
    margin:0;
    padding:0;
}

.navone ul ul {
    display: none;

}

.navone ul li:hover > ul {
    display: block;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navone ul {
    list-style: none;
    position: relative;
    display: inline-table;
}

.navone ul:after {
    content: ""; clear: both; display: block;
}

.navone ul li {
    float: left;
    cursor:pointer;
    padding:10px 20px;
}

.navone ul li:hover {
    background:#000;
}

.navone ul li:hover a {
    color: #fff;
}
    
.navone ul li a {
    display: block;
    color: #FFF;
    text-decoration: none;
}

.navone ul li ul {
    width:200px;
    z-index:9;
}

.navone ul ul {
    background: #000;
    padding: 0;
    position: absolute;
    top:100%;
    left:0;
}

.navone ul ul li {
    float: none; 
    position: relative;
    padding:5px 10px;
}

.navone ul ul li a {
    color: #fff;
}

.navone ul ul li a:hover {
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navone ul ul ul {
    position: absolute; left: 100%; top:0;
}

.navone ul li ul li {
    padding:8px 10px;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}
.navone ul li ul li:hover {
    border-left:5px solid #F52100;
    padding-left:20px;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navtwo {
    display:inline;
}

IF anyone could help me or point out what i’m missing because my code doesn’t work. My code doesn’t have errors but Im not achieving what I want to accomplish.

Could anyone extend their helping hand.

Related posts

3 comments

  1. <ul>
    <?php $args = array(
            'type'                     => 'post',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false
    
    );
    
    $categories = get_categories( $args );
    
    foreach($categories as $cat)
    { 
        if ($cat->category_parent == 0) {
    
    ?>
        <li>
            <?php echo $cat->name; $cat_id = $cat->term_id;?>
            <?php $post_args = array(
                'post_type'=>'post',
                'posts_per_page' => 3,
                'cat' => $cat_id
            );
            $the_query = new WP_Query($post_args);
            if($the_query->have_posts()): ?>
            <ul>
                <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                        <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </li>
                <?php endwhile; ?>
            </ul>
            <?php endif; wp_reset_query(); ?>
        </li>
    <?php } }?>
    </ul>
    
  2. You can try this:

    <ul>
    <?php $args = array(
            'type'                     => 'post',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false
    
    );
    
    $categories = get_categories( $args );
    
    foreach($categories as $cat): ?>
        <li>
            <?php echo $cat->$name; $cat_id = $cat->$term_id;?>
            <?php $post_args = array(
                'post_type'=>'post',
                'posts_per_page' => 3,
                'cat' => $cat_id
            );
            $the_query = new WP_Query($post_args);
            if($the_query->have_posts()): ?>
            <ul>
                <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                        <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </li>
                <?php endwhile; ?>
            </ul>
            <?php endif; wp_reset_query(); ?>
        </li>
    <?php endforeach; ?>
    </ul>
    

    It’s not tested but it should work.

  3. You have been missing few things in the above code .

    Let’s try to make it simple.

    Step 1 : Get all the parent categories and child categories under them inside an array named as $total_categories

    You can see the code below to get the array for all parent categories and child categories

    <?php 
    $parent_args = array(
            'type'                     => 'post',
            'parent'                   => '0',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0,
            'hierarchical'             => 1,
            'taxonomy'                 => 'category',
            'pad_counts'               => false
    );
    $parent_categories = get_categories( $parent_args );
    
    foreach ($parent_categories as $parent_category) {
        $child_args = array(
            'type'                     => 'post',
            'parent'                   => $parent_category->term_id,
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0,
            'hierarchical'             => 1,
            'taxonomy'                 => 'category',
            'pad_counts'               => false
        );  
        $cats_arr = array(); 
        $child_categories = get_categories( $child_args ); 
        $cats_arr[] = $parent_category->term_id;
        foreach ($child_categories as $child_category) {
            $cats_arr[] = $child_category->term_id;
        }
    
        $total_categories[$parent_category->term_id] = $cats_arr;
    }
    ?>
    

    Output Provided was for my case

    Above array will look as where 18 is the parent category ID and the child categories are 19 and 20 plus we have also included the parent category ID (18) in the array as well as index of the array.

    Array
    (
        [18] => Array
            (
                [0] => 18
                [1] => 19
                [2] => 20
            )
    
        [15] => Array
            (
                [0] => 15
                [1] => 16
                [2] => 17
            )
    
        [1] => Array
            (
                [0] => 1
            )
    
    )
    

    Now simply traverse the array $total_categories to get all the parent categories and top 3 posts under that categories

    <ul>
    <?php foreach($total_categories as $total_category_k=>$total_category_v): ?>
            <li>
    
                <a href=""><?php echo get_the_category_by_ID($total_category_k);?></a>
            <?php $post_args = array(
                'post_type'=>'post',
                'posts_per_page' => 3,
                'orderby'=>'ID',
                'order'=>'DESC',
                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'category',
                                        'field'    => 'term_id',
                                        'terms'    => $total_category_v,
                                        'operator' => 'IN'
                                    ),
                                ),
                );
    
            $the_query = new WP_Query($post_args);
            if($the_query->have_posts()): ?>
            <ul>
                <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                        <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </li>
                <?php endwhile; ?>
            </ul>
            <?php endif; wp_reset_query(); ?>
        </li>
    <?php endforeach; ?>
    </ul>
    

    Output Provided was for my case

    And it will finally give you the desired HTML with required content output as below :

    <ul>
            <li>
    
                <a href="">menu</a>
                </li>
            <li>
    
                <a href="">test</a>
                </li>
            <li>
    
                <a href="">Uncategorized</a>
                    <ul>
                                    <li>
                            <a href="http://localhost/wp/clark_atlanta_university/students-unlock-the-mysteries-of-the-brain-with-nih-scientists/">Students unlock the mysteries of the brain with NIH scientists</a>
                        </li>
                                    <li>
                            <a href="http://localhost/wp/clark_atlanta_university/contacts-better-than-permanent-lenses-for-babies-after-cataract-surgery/">Contacts better than permanent lenses for babies after cataract surgery</a>
                        </li>
                                    <li>
                            <a href="http://localhost/wp/clark_atlanta_university/nih-announces-recruitment-for-clinical-trial-to-test-new-tinnitus-treatment-device/">NIH announces recruitment for clinical trial to test new tinnitus treatment device</a>
                        </li>
                        </ul>
                </li>
    </ul>
    

    The code you were missing was :

    'orderby'=>'ID',
    'order'=>'DESC',
    

    To get the top 3 posts you will have to get the posts in descending
    order by ID and of course posts_per_page will get you 3 records only

    'tax_query' => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'term_id',
                                            'terms'    => $total_category_v,
                                            'operator' => 'IN'
                                        ),
                                    ),
                    );
    

    And to get posts related to categories (whether the parent or child )
    so we have passed the array containing the parent as well as child
    categories

Comments are closed.