0
votes

I have to echo out all categories with posts that are inside my custom post type that ia have created. I have currently three categories and i need it because i might add another category and thats why i dont have to dive in to coding to display another query.

Style:

- Category title
-- category post title, content etc.

- Category2 title
-- Category2 post contents

My current code looks like that

        <?php
        $args=array(
          'post_type' => 'edasimja'
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

        <div class="col-sm-4">
            <?php $c = get_the_category(); ?>
            <?php echo $c[0]->cat_name; ?>
          <h2><?php the_title(); ?></h2>
          <?php the_field('eli'); ?>
        </div>

            <?php
          endwhile;
        }
        wp_reset_query();
        ?>

This code querys every single post with its cat name separately, but i need a column with every category. I cant seem to achieve that.

Can someone point me to the right direction?

1
Can you show us what this code is generating? - Mike. D
My code generated blocks for every post and adding cat name to it. So if i had two posts inside cat "knives" then it will be adding that cat name "knives" to two posts and not combining them to one block. - StackSurfer

1 Answers

1
votes

So, i have found a solution for it.

    <?php

    $post_type = 'features';
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

    foreach( $taxonomies as $taxonomy ) :

// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );

foreach( $terms as $term ) : ?>

    <section class="category-section">

    <div class="row">
    <div class="span12">
        <h2 class="mid-heading"><?php echo $term->name; ?></h2>
    </div>

    <?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() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

        <div class="span4">

            <article class="inner-post clearfix">

                <div class="inner-img whitebox">
                <?php if(has_post_thumbnail()) { ?>
                        <?php the_post_thumbnail(); ?>
                <?php }
                /* no post image so show default */
                else { ?>
                       <img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" />
                <?php } ?>
                </div>

                <div class="inner-content">

                <h3 class="heading-size-14 font-weight-600"><a href="<?php echo get_permalink(); ?>" title="Read more about <?php echo get_the_title(); ?>"><?php  echo get_the_title(); ?></a></h3>

                    <?php the_excerpt(); ?>
                </div>
            </article><!-- about-box -->


        </div>

    <?php endwhile; endif; ?>
    </div>
    <hr>
    </section>

<?php endforeach;

endforeach; ?>