Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple mini post loop using on get_post and passing args for category id, qnt, and thumbs excerpts, intended for use in pages primarilly to render a light cat list of title links.
/*
* Shortcode get_post loop
*
* Shortcode displays loops for categories in posts or pages.
* It takes params for cat ID, qnt, show_thumbs, show_excerpt & height / width.
*
* [hnla_cat_loop cat_id='4' qnt='4' show_thumb=true show_excerpt=true, height='120', width='120' ]
*
* If showing thumbnails additional markup elements wrap the title and excerpt to allow positioning to right side of
* floated thumbnail.
*/
function post_cat_loop($atts) {
//global $post;
extract(shortcode_atts(array(
'cat_id' => '',
'qnt' => '2',
'show_excerpt' => false,
'show_thumb' => false,
'width' => '80',
'height' => '80'
), $atts));
//var_dump($show_excerpt);
$hnla_cat_list = get_posts( array(
'category' => $cat_id,
'numberposts' => $qnt
) );
ob_start();
?>
<div id="news-list">
<?php foreach( $hnla_cat_list as $cat_list ) : setup_postdata($cat_list); ?>
<div class="cat-post ">
<?php if( true == $show_thumb && has_post_thumbnail( $cat_list->ID ) ) :
echo '<a href="'. get_permalink( $cat_list ) .'">'. get_the_post_thumbnail( $cat_list->ID, array( $height , $width ), 'thumbnail' ) .'</a>'; ?>
<div class="entry-block">
<?php endif; ?>
<h3 class="news-title"><a href="<?php echo get_permalink( $cat_list ) ?>"><span>&gt; </span><?php echo esc_html( $cat_list->post_title ) ?></a></h3>
<?php if(true == $show_excerpt) : ?>
<div class="entry">
<?php the_excerpt() ?><?php //var_dump( $cat_list ); ?>
</div>
<?php endif; ?>
<?php if( true == $show_thumb && has_post_thumbnail( $cat_list->ID ) ) : ?>
</div><!-- / .entry-block -->
<?php endif; ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div><!-- /#news-list -->
<?php
$the_short_loop = ob_get_contents();
ob_end_clean();
return $the_short_loop;
}
add_shortcode('hnla_cat_loop', 'post_cat_loop');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.