Last active
October 20, 2023 09:21
-
-
Save thetwopct/76a2a19aa362b9f0b3928d739fd5333f to your computer and use it in GitHub Desktop.
Loop through CPT categories and display posts within
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Loop through Categories and Display Posts within | |
https://wordimpress.com/loop-through-categories-and-display-posts-within/ | |
*/ | |
$post_type = 'treatments'; | |
// Get all the taxonomies for this post type | |
$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 | |
'order' => 'ASC', | |
'orderby' => 'meta_value, title', | |
'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=""><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; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much… this is the only way! easy, best man. There is so much bullshit, but this works!!!!