Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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