Displaying CPT entries grouped by Custom Taxonomies in Genesis. https://sridharkatakam.com/displaying-cpt-entries-grouped-by-custom-taxonomies-in-genesis/
<?php | |
add_action( 'genesis_before_loop', 'sk_check_for_terms' ); | |
function sk_check_for_terms() { | |
$taxonomy_name = 'kbcategory'; //specify your custom taxonomy here | |
// Retrieve the terms in the above custom taxonomy | |
$tax_terms = get_terms( $taxonomy_name ); | |
// if there's at least 1 taxonomy term, then replace the default loop with a custom one | |
if ( ! empty( $tax_terms ) && ! is_wp_error( $tax_terms ) ) { | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'sk_do_loop' ); | |
} | |
} | |
function sk_do_loop() { | |
$taxonomy_name = 'kbcategory'; //specify your custom taxonomy here | |
// Retrieve the terms in the above custom taxonomy | |
$tax_terms = get_terms( $taxonomy_name ); | |
echo '<div class="posts-list">'; | |
foreach( $tax_terms as $tax_term ) { | |
echo '<div class="row">'; | |
echo '<h2>' . $tax_term->name . '</h2>'; | |
$args = array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => $taxonomy_name, | |
'field' => 'slug', | |
'terms' => array( $tax_term->slug ) | |
) | |
), | |
// 'posts_per_page' => 3, | |
); | |
$query = new WP_Query( $args ); | |
echo '<ul>'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
echo '<li><h5><a href="' . get_permalink() . '">'. get_the_title() .'</a></h5></li>'; | |
} | |
echo '</ul>'; | |
// Uncomment the lines of code (not comments) below to display a link to custom taxonomy archive | |
/* Always check if it's an error before continuing. get_term_link() can be finicky sometimes */ | |
// $tax_term_link = get_term_link( $tax_term, $taxonomy_name ); | |
// if( is_wp_error( $tax_term_link ) ) | |
// continue; | |
/* We successfully got a link. Print it out. */ | |
// echo '<p class="more-from-link"><a href="' . $tax_term_link . '">More from ' . $tax_term->name . ' category »</a></p>'; | |
wp_reset_query(); | |
echo '</div>'; | |
} // end foreach | |
echo '</div>'; | |
} | |
genesis(); |
<?php | |
add_action( 'genesis_before_loop', 'sk_check_for_terms' ); | |
function sk_check_for_terms() { | |
$taxonomy_name = 'kbcategory'; //specify your custom taxonomy here | |
// Retrieve the terms in the above custom taxonomy | |
$tax_terms = get_terms( $taxonomy_name ); | |
// if there's at least 1 taxonomy term, then replace the default loop with a custom one | |
if ( ! empty( $tax_terms ) && ! is_wp_error( $tax_terms ) ) { | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'sk_do_loop' ); | |
} | |
} | |
function sk_do_loop() { | |
$taxonomy_name = 'kbcategory'; //specify your custom taxonomy here | |
// Retrieve the terms in the above custom taxonomy | |
$tax_terms = get_terms( $taxonomy_name ); | |
echo '<div class="posts-list">'; | |
foreach( $tax_terms as $tax_term ) { | |
echo '<div class="row">'; | |
echo '<h2>' . $tax_term->name . '</h2>'; | |
$args = array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => $taxonomy_name, | |
'field' => 'slug', | |
'terms' => array( $tax_term->slug ) | |
) | |
), | |
// 'posts_per_page' => 3, | |
'order' => 'ASC', | |
'orderby' => 'title' | |
); | |
$query = new WP_Query( $args ); | |
echo '<ul>'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
echo '<li><h5><a href="' . get_permalink() . '">'. get_the_title() .'</a></h5></li>'; | |
} | |
echo '</ul>'; | |
// Uncomment the lines of code (not comments) below to display a link to custom taxonomy archive | |
/* Always check if it's an error before continuing. get_term_link() can be finicky sometimes */ | |
// $tax_term_link = get_term_link( $tax_term, $taxonomy_name ); | |
// if( is_wp_error( $tax_term_link ) ) | |
// continue; | |
/* We successfully got a link. Print it out. */ | |
// echo '<p class="more-from-link"><a href="' . $tax_term_link . '">More from ' . $tax_term->name . ' category »</a></p>'; | |
wp_reset_query(); | |
echo '</div>'; | |
} // end foreach | |
echo '</div>'; | |
} | |
genesis(); |
/* Displaying CPT entries grouped by Custom Taxonomies | |
------------------------------------------------------- */ | |
.posts-list { | |
background: #fff; | |
padding: 50px 60px; | |
margin-bottom: 40px; | |
} | |
.posts-list .row { | |
margin-bottom: 40px; | |
} | |
.posts-list .row:last-child { | |
margin-bottom: 0; | |
} | |
.row h2 { | |
margin-bottom: 20px; | |
} | |
.more-from-link { | |
margin-top: 20px; | |
margin-bottom: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment