<div id="tab-block" class="tab-block"> <div class="outer-tab-caption"></div> <?php $args = array('parent' => '0', 'orderby' => 'slug'); foreach (get_terms('category', $args) as $category) { ?> <div class="outer-tab-content"> <div class="undertitle"><?php echo $category->name; ?> </div> <?php echo projects_tree($category); ?> </div> <?php } ?> </div> </div> </section> <section id="box-5" class="section box-5" data-section-name="What do we offer"> <div class="box-bg"></div> <div class="content"> <h1>What do we offer</h1> <div class="row">
function projects_tree($category, $i = 0) { $i++; $sub_cat_ids = get_term_children($category->term_id, $category->taxonomy); if (!empty($sub_cat_ids)) { ob_start(); echo '<div class="inner-tab-caption"></div>'; foreach ($sub_cat_ids as $sub_cat_id) { //get object of child by id $sub_cat = get_term_by('id', $sub_cat_id, $category->taxonomy); echo '<div class="inner-tab-content">'; echo '<div class="subundertitle">' . $sub_cat->name . '</div>'; //recursion echo projects_tree($sub_cat, $i); echo '</div>'; } return ob_get_clean(); } else { if ($i == 1) { echo '<div class="inner-tab-caption empty"></div>'; } ob_start(); $args = array('cat' => $category->term_id, 'posts_per_page' => 5); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $img_id = get_post_thumbnail_id($post_id); $img_attr = wp_get_attachment_image_src($img_id, 'medium-image'); $img_url = $img_attr[0]; $img_alt = get_post_meta($img_id, '_wp_attachment_image_alt', true); ?> <a class="item js-overly-trigger js-single-ajax" href="<?php echo get_permalink(); ?> " data-project="<?php echo $post_id; ?> " data-overlay="#push-page"> <figure class="item-img"> <img src="<?php echo $img_url; ?> " alt="<?php echo $img_alt; ?> "/> </figure> <div class="item-content"> <!-- <div class="item-container">--> <h3 class="item-title"><?php the_title(); ?> </h3> <span class="item-button">Посмотреть проект</span> <!-- </div>--> </div> </a> <?php } } wp_reset_postdata(); return '<div class="owl-gallery">' . ob_get_clean() . '</div>'; } }