Skip to content

Instantly share code, notes, and snippets.

@webmasterninjay
Created March 20, 2015 11:40
Show Gist options
  • Save webmasterninjay/47974b14475dc882ece6 to your computer and use it in GitHub Desktop.
Save webmasterninjay/47974b14475dc882ece6 to your computer and use it in GitHub Desktop.
Wrdpress: WP Query shortcode
<?php
/*
* Call using [jay-home-recent/] shortcode
*/
// Home recent shortcode
add_shortcode( 'jay-home-recent', 'jay_post_listing_shortcode' );
function jay_post_listing_shortcode($atts) {
ob_start();
$args = array(
'post_type' => 'post',
'showposts' => 2,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<div class="home-recent-posts">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php the_post_thumbnail( 'hr-thumbnail', array( 'class' => 'alignright' ) ); ?>
</a>
<?php endif; ?>
<div class="hr-entry-title">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div>
<div class="hr-entry-meta">
<p><?php the_time('F jS, Y'); ?> / by <?php the_author_posts_link(); ?> / <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>
</div>
<div class="hr-entry-content">
<?php
echo '<p>';
$content = get_the_content();
// TRIM the $content
$trimmed_content = wp_trim_words( $content, 20, '...</p><p class="hr-read-more"><a href="'. get_permalink() .'" rel="bookmark" title="'.get_the_title().'" class="button">Continue reading</a></p>' );
echo $trimmed_content;
?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<?php $thepost = ob_get_clean();
return $thepost;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment