Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active December 26, 2021 20:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diggeddy/b71bf07aa55eecb0c34191f9fe05d224 to your computer and use it in GitHub Desktop.
Save diggeddy/b71bf07aa55eecb0c34191f9fe05d224 to your computer and use it in GitHub Desktop.
Custom Loop Shortcode for displaying a GPP Content Template
<?php
function db_custom_loop_shortcode($atts, $content = null) {
global $post;
// Set query args
$args = array(
'post_type' => 'post',
'posts_per_page' => '3',
'post__not_in' => array( $post->ID ), // don't display current post
);
// Optional arguments for setting category term relationship on single post
if ( is_single() && has_category() ) {
$category = get_the_category($post->ID);
$category_id = $category[0]->cat_ID;
$category_count = $category[0]->count;
if ( $category_count > 1 ) {
$args['category__in'] = array($category_id);
}
}
$latest = new WP_Query($args);
// Output loop
if ( $latest->have_posts() ) {
ob_start();
echo '<div class="gb-grid-wrapper gb-grid-wrapper-global">';
while ($latest->have_posts()) : $latest->the_post();
echo '<div class="gb-grid-column gb-grid-column-global-33">';
do_action('db_custom_post_loop'); // Hook name for content template
echo '</div>';
endwhile;
echo '</div>';
wp_reset_postdata();
return ob_get_clean();
}
}
add_shortcode('db_display_custom_post_loop', 'db_custom_loop_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment