最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Loop through categories and display posts title under each dropdown

programmeradmin0浏览0评论

I want to loop through categories and display the post's title under each category dropdown. Clicking the post name from the dropdown will display the post content in a tab pane in WordPress. Can someone please suggest a code solution.

    <?php
    $args = array(
        'taxonomy' => 'types',
        'orderby' => 'name',
        'order'   => 'ASC'
    );
    $cats = get_categories($args);
    $c = 1;
    foreach($cats as $cat) {
?>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle <?=($c == 1) ? 'active' : ''?>" href="#<?php echo $cat->name; ?>" role="button" data-toggle="tab" aria-haspopup="true" aria-expanded="false"><?php echo $cat->name; ?></a>
            <div class="dropdown-menu">
<?php
    $cat_slug = $category->slug;
    $the_query = new WP_Query(array(
        'post_type' => 'project',
        'category_name' => $cat_slug
    )); 
    while($the_query->have_posts()){
    $the_query->the_post();
?>
                <a class="dropdown-item" href="#<?php the_ID(); ?>" data-toggle="tab"><?php the_title(); ?></a>
<?php }
wp_reset_postdata();
?>
            </div>
        </li>
<?php
    $c++;
}
wp_reset_postdata();
?>
发布评论

评论列表(0)

  1. 暂无评论