请原谅我正在学习PHP和Wordpress。 我正在尝试在wordpress中创建一个下拉菜单,并将每个类别作为菜单选项,并将该类别中每个帖子的标题作为子菜单。 我让类别正确显示,但我无法弄清楚如何只让该特定类别帖子显示在正确的类别下。
$args = array('post_type' => 'talent');
$the_query = new WP_Query( $args );
foreach (get_categories('hide_empty=0&exclude=1') as $category){
echo "<li class='has-children'><a href='#'>";
echo $category->name;
echo "</a><ul class='is-hidden'>";
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo "<li><a href='#0'>";
echo the_title() ;
echo "</a></li>";
endwhile;
else: endif;
echo wp_reset_query();
echo "</ul></li>";
}
试试这个:-
$args = array('post_type' => 'talent');
$the_query = new WP_Query( $args );
foreach (get_categories('hide_empty=0&exclude=1') as $category){
echo "<li class='has-children'><a href='#'>";
echo $category->name;
echo "</a><ul class='is-hidden'>";
$args = array( 'category' => get_cat_ID($category->name), 'post_type' => 'talent' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
echo wp_reset_query();
echo "</ul></li>";
}
上面 foreach 将为您提供该特定类别的帖子。然后,您可以根据需要对其进行自定义。希望它能解决这个问题。
$args = array('post_type' => 'talent','
cat' => array(48,43,49,46,47,44,51,50,42((;
您只需在数组中添加 cat 和要显示的类别数量即可根据特定类别获取帖子