dongshou2017 2018-08-07 15:51
浏览 45
已采纳

循环多个帖子类型和帖子foreach

What i want to make:

I want to make a navigation menu where every dropdownbar is its own post type.

  1. Movie
    • movie1
    • movie2
  2. Book
    • book1
    • book2
  3. Game
    • game1
    • game2

What i've made so far:

I'm not the best at php yet, but i tried to work something out:

echo "<ul class="menu">";

$post_type = get_post_types( array('Movie', 'Book', 'Game') );

foreach( $post_type as $type ) {

    $args = array(
        'post_type' => $type
    );

    echo "<li>".$type."<ul class="dropdown">";

    $posts = get_posts( $args );

    if( $posts ) {

        foreach( $posts as $post ) {

            echo "<li>".get_the_title( $post->ID, 'title' )."</li>";

        }
    echo "</ul></li>";
    }
}
echo "</ul>";

Question:

Is there a smarter way to make the dropdownmenu? or what can i do to make it work?

  • 写回答

1条回答 默认 最新

  • douding_1073 2018-08-08 10:11
    关注

    There's nothing wrong with that approach, except that you shouldn't use get_post_types() - just an array of the post type names will do.

    As it stands at the moment, 'post_type' => $type will pass an array to post_type, when it should be a string.

    Also, echo "<li>".$type."<ul class="dropdown">"; should be inside your if( $posts ) { before the foreach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料