Last active
April 30, 2022 03:42
-
-
Save FernE97/2975895 to your computer and use it in GitHub Desktop.
PHP: WordPress custom taxonomy/post query
This file contains hidden or 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 | |
$args = array( | |
'orderby' => 'ID' | |
); | |
$terms = get_terms( 'testimonial_category', $args ); | |
?> | |
<!-- bootstrap tabs --> | |
<ul class="nav-tabs"> | |
<?php | |
$count = 0; | |
foreach ( $terms as $term ) : $count ++; ?> | |
<li<?php if ( $count == 1 ) echo ' class="active"' ?>> | |
<a href="#<?php echo $term->slug ?>" data-toggle="tab"><?php echo $term->name ?></a> | |
</li> | |
<?php | |
endforeach; ?> | |
</ul> | |
<div class="tab-content"> | |
<?php | |
$count = 0; | |
foreach ( $terms as $term ) : $count ++; ?> | |
<div class="tab-pane <?php if ( $count == 1 ) echo 'active' ?>" id="<?php echo $term->slug ?>"> | |
<?php | |
$args = array( | |
'post_type' => 'testimonial', | |
'tax_query' => array( | |
array( | |
'taxonomy' => $term->taxonomy, | |
'field' => $term->slug, | |
'terms' => $term->term_id | |
) | |
) | |
); | |
$loop = new WP_Query( $args ); | |
if ( $loop->have_posts() ) : | |
while ( $loop->have_posts() ) : $loop->the_post(); ?> | |
<blockquote class="testimonial-<?php the_ID(); ?>"> | |
<?php the_content(); ?> | |
</blockquote> | |
<cite><?php the_title(); ?></cite> | |
<?php | |
endwhile; | |
endif; wp_reset_query(); | |
?> | |
</div><!-- end tab-pane --> | |
<?php endforeach; ?> | |
</div><!-- end tab-content --> |
Thank you for the code..It saved me hours :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is using the bootstrap-tab plugin and pulls in each category for the tab area, the content area then pulls in any posts that are listed under each category.