我从抄本上了解到(https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops).
然后我试着列出如下帖子:
<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>
如果您注意到,我正在调用一个名为get_posts_menus()
.我使用的函数是:
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;
这里发生的事情是,它只会给我们回最后的帖子3次。但主要的想法是,这将返回该类别的3个最新帖子。
我对此感到非常困惑,我开始考虑使用JSON而不是wp\\U查询。
非常感谢。