What I understand from the CODEX, (https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops).
then I'm trying to do a list of posts like this:
<nav id="cbp-hrmenu" class="cbp-hrmenu main-menu">
<ul>
<li>
<a href="#">Secciones</a>
<div class="cbp-hrsub">
<div class="cbp-hrsub-inner">
<div>
<h4><a href="#" class="menu-section">Topic 1</a></h4>
<ul>
<?php echo get_posts_menus('topic-1','3'); ?>
</ul>
<h4><a href="#" class="menu-section">Topic 2</a></h4>
<ul>
<?php echo get_posts_menus('topic-2','3'); ?>
</ul>
</div>
<div>
<h4><a href="#" class="menu-section">Topic 3</a></h4>
<ul>
<?php echo get_posts_menus('topic-3','3'); ?>
</ul>
<h4><a href="#" class="menu-section">Topic 4</a></h4>
<ul>
<?php echo get_posts_menus('topic-4','3'); ?>
</ul>
</div>
</li>
</ul>
</nav>
If you notice , I'm calling a function named get_posts_menus()
.
Where the function I'm using is:
if (!function_exists('get_posts_menus')):
function get_posts_menus($category_name, $count)
{
$permalink = get_the_permalink();
$title = get_the_title($the_menu->post->ID);
$post_class = get_the_id();
$args = array(
'category_name' => $category_name,
'posts_per_page' => $count
);
$the_menu = new WP_Query($args);
while ($the_menu->have_posts()) {
$the_menu->the_post();
echo "<li class=\"post-id-$post_class\"><a href=\"$permalink\" alt=\"$title\">$title</a></li>";
}
wp_reset_postdata();
}
endif;
What happened here , is that it will give us back only the last post 3 times.
But the main idea is that this give back the 3 latest post of the category.
I'm very confused about this , that I'm start thinking of using JSON instead of the wp_query.
thank you