Created
June 9, 2013 22:04
-
-
Save xafarali/5745441 to your computer and use it in GitHub Desktop.
Add Recent Post shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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