0

I am trying to create a slider with taxonomy post categories and its Posts so the structure will look like this

Row 1
    First Category Title
      First category Post Title Goes Here
    Second Category Title
      Second category Post Title Goes Here
Row 2
    Third Category Title
      Third category Post Title Goes Here
    Fourth Category Title
      Fourth category Post Title Goes Here

So what i tried currently for getting the taxonomy category and its posts is teh below snippet

<div class="clientsListRow">
                    <?php 
                        $post_type = 'clients';
                        $taxonomies = get_object_taxonomies(array( 'post_type' => $post_type));
                        foreach($taxonomies as $taxonomy) :
                            $terms = get_terms($taxonomy);
                            foreach($terms as $term) : ?>
                              <ul><li><a><?php echo $term->name; ?></a>
                                <?php
                                $args = array(
                                    'post_type' => $post_type,
                                    'posts_per_page' => -1,  //show all posts
                                    'tax_query' => array(
                                        array(
                                            'taxonomy' => $taxonomy,
                                            'field' => 'slug',
                                            'terms' => $term->slug,
                                        )
                                    )
                                );
                                $posts = new WP_Query($args);
                                if($posts->have_posts()): ?>
                                <ul>
                                <?php  while($posts->have_posts() ) : $posts->the_post(); ?>
                                    <li><a><?php  echo get_the_title(); ?></a></li>
                                <?php endwhile; endif; ?>
                                </ul></li></ul>
                            <?php endforeach;
                        endforeach; 
                    ?>
                </div>

But could not manage to create the separating part with a div so i wrap every row with X amount of uls in a div.

ANy ideas how i achieve this?

  • 1
    Generally speaking you need to replace your foreach loops with incrementing loops. So instead of foreach you will have for($i=0; $i<count($terms); $i++) etc. If you can add your exact desired HTML output I think I can help you achieve it. I assume each row needs an outer <div class="row"> then within the row I am asking what structure you need - should category titles be <h2>s and post titles be wrapped inside <div>s? – WebElaine Apr 3 '17 at 15:40
  • The general output is to have 6 categories in each row with their subcategories. I have categories in ul li so <div class="row1"><ul><li><a></a><ul>with its subcats</ul></li></ul></div>. I also need to separate each 3 categories inside that one row. Thank you for the help @WebElaine – Anahit DEV Apr 4 '17 at 6:12
0

This should get you pretty close. The key is to use iterating loops and check which iteration you're on to determine whether or not to output a new <div class="row">, open or close a <ul>, etc.

Note that one you are in a loop, you can just use the_permalink() and the_title() because the query already grabbed them, no need to echo get_the_title() etc.

<div class="clientsListRow"><?php 
    $post_type = 'clients';
    $taxonomies = get_object_taxonomies(array('post_type' => $post_type));
    foreach($taxonomies as $taxonomy) :
        $terms = get_terms($taxonomy);
        for($t=1; $t<=count($terms); $t++) {
            // if "t" is 1 or a multiple of 7, start a new row
            if($t == 1 || $t%7 == 0) { ?><div class="row"><?php }
                // if "t" is 1 or a multiple of 4, start a new list
                if($t == 1 || $t%4 == 0) { ?><ul><?php } ?>
                <li>
                    <a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a>
                    <?php
                    $args = array(
                        'post_type' => $post_type,
                        'posts_per_page' => -1,  //show all posts
                        'tax_query' => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                            )
                        )
                    );
                    $posts = new WP_Query($args);
                    if($posts->have_posts()) : ?>
                        <ul>
                        <?php while($posts->have_posts() ) : $posts->the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
                </li>
            <?php // if "t" is a multiple of 3, or the very last item, close the list
            if($t%3 == 0 || $t == count($terms)) { ?></ul><?php
            // if "t" is a multiple of 6, or the very last item, close the row
            if($t%6 == 0 || $t == count($terms)) { ?></div><!-- end row --><?php
        }
    endforeach; ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.