Php 如何使用';foreach&x27;在wordpress中自定义菜单?

Php 如何使用';foreach&x27;在wordpress中自定义菜单?,php,wordpress,Php,Wordpress,请原谅我,我正在学习PHP和Wordpress。我试图在wordpress中创建一个下拉菜单,将每个类别作为菜单选项,并将该类别中每个帖子的标题作为子菜单。我有适当的类别显示,但因为我不知道如何只让特定类别的帖子显示在正确的类别下 $args = array('post_type' => 'talent'); $the_query = new WP_Query( $args ); foreach (get_categories('hide_empty=0&exclude=1') a

请原谅我,我正在学习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');
$thew_query=newwp_query($args);
foreach(获取类别('hide\u empty=0&exclude=1')作为$category){
回声“
  • ”; }
    $args=array('post_type'=>'talent','cat'=>array(48,43,49,46,47,44,51,50,42))

    您只需在数组中添加cat和要显示的类别数,即可根据特定类别获得帖子。请尝试以下操作:-

    $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>";
    }
    
    $args=array('post_type'=>'talent');
    $thew_query=newwp_query($args);
    foreach(获取类别('hide\u empty=0&exclude=1')作为$category){
    回声“
  • ”; }

    上面的foreach将为您提供该特定类别的帖子。然后,您可以根据自己的要求进行定制。希望它能解决这个问题

    谢谢,不过我不知道我是否清楚。我想将类别作为菜单,将条目作为子菜单,而不必对每个类别进行编码。因此,理想情况下,如果我添加一个新类别,它将显示在菜单上。如果我向任何类别添加新条目,它将显示在相应的子菜单上,如果我没有弄错的话,使用这种方法,我需要为每个类别创建一个不同的数组?嗯,不幸的是,这仍然会为“人才”添加每个条目在每个类别下…你能回显$category->id并查看你是否从中获得cat_id吗?你可以查看此链接。。试试这个$类别id=获取类别id($category->name);然后使用这个变量作为cat_id。对不起,我应该把它放在哪里,我要替换什么?我很抱歉!我要发疯了,我想把我的头缠在这上面