Skip to content

Instantly share code, notes, and snippets.

@xafarali
Created June 9, 2013 22:04
Show Gist options
  • Save xafarali/5745441 to your computer and use it in GitHub Desktop.
Save xafarali/5745441 to your computer and use it in GitHub Desktop.
Add Recent Post shortcode
// Shortcode for recent post
add_shortcode('ss_recentpost','ss_recent_post');
// add_filter('the_content', 'do_shortcode', 11);
add_filter('widget_text', 'do_shortcode');
function ss_recent_post($attr) {
global $post;
ob_start();
$setting = shortcode_atts(array(
'numberposts' => 3,
'category' => null,
'thumbnail' => false,
'length' => 10
), $attr );
extract($setting);
$output = "";
$arg = "numberposts=".$numberposts;
if($category) {
$category_id = get_cat_ID($category);
$arg .="&category=".$category_id;
}
$posts = get_posts($arg);
foreach( $posts as $post ) {
setup_postdata( $post );
?>
<div class="recent-post-wigdet">
<?php
if ( has_post_thumbnail() && $thumbnail == true ) : ?>
<span class="post-thumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('small-thumbnail'); ?></a>
</span>
<?php endif;?>
<div class="recent-post-container">
<h4><a href="<?php the_permalink($post-ID) ?>"><?php the_title() ?></a></h4>
<p>
<?php echo wp_trim_words( $post->post_content, $length, $more = "<span>...</span>" ); ?>
</p>
<span class="smore"><a href="<?php echo the_permalink($post-ID) ?>">Read More </a></span>
</div>
</div>
<?php
}
// to retur anywhere in Contents
$output = ob_get_contents();
ob_end_clean();
return $output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment