Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Last active August 7, 2023 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AchalJ/fd41be2159cbe66bcd02b286d4c5bbcf to your computer and use it in GitHub Desktop.
Save AchalJ/fd41be2159cbe66bcd02b286d4c5bbcf to your computer and use it in GitHub Desktop.
PowerPack Content Grid - Group post terms by taxonomies in meta
<?php // ignore this.
// Instructions:
// 1. Add the following CSS class to the Content Grid module: cg-with-meta-tags
// 2. Add the below code to your current theme's functions.php file.
add_action( 'pp_cg_before_post_content', function( $post_id, $settings ) {
if ( false === strpos( $settings->class, 'cg-with-meta-tags' ) ) {
return;
}
if ( ! isset( $terms_list ) ) {
$terms_list = wp_get_post_terms( $post_id, $settings->post_taxonomies );
$terms_list = is_wp_error( $terms_list ) ? array() : $terms_list;
}
if ( empty( $terms_list ) ) {
return;
}
?>
<div class="pp-content-post-category pp-post-meta">
<?php
$sorted = array();
foreach ( $terms_list as $term ) {
$key = $term->taxonomy . '_' . $term->name;
if ( ! isset( $sorted[ $key ] ) ) {
$sorted[ $key ] = $term;
}
}
ksort( $sorted );
$i = 1;
foreach( $sorted as $term ) {
$class = ( isset( $term->parent ) && $term->parent > 0 ) ? ' child-term' : ' parent-term';
?>
<span class="pp-post-meta-term term-<?php echo $term->slug; ?><?php echo $class; ?>"><?php echo $term->name; ?></span>
<?php if ( $i != count( $terms_list ) ) { ?>
<span class="pp-post-meta-separator"><?php echo $settings->meta_separator; ?></span>
<?php } ?>
<?php
$i++;
}
?>
</div>
<?php
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment