Skip to content

Instantly share code, notes, and snippets.

@phil-hudson
Created August 19, 2016 03:34
Show Gist options
  • Save phil-hudson/4a8883451cf43c0c1090aee2688a39b3 to your computer and use it in GitHub Desktop.
Save phil-hudson/4a8883451cf43c0c1090aee2688a39b3 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Recent Posts Shortcode
* Plugin URI: http://www.phil-hudson.com/recent-posts-shortcode-wordpress-plugin/
* Description: A simple, open-source plugin to return your most recent posts via a shortcode. Use on any post or page: [recentPosts amountOfPosts="2"]. For more details please see: http://phil-hudson.com/recent-posts-shortcode-wordpress-plugin/
* Version: 0.1
* Stable: 4.2.2
* Author: Phil Hudson
* Author URI: http://www.phil-hudson.com
* License: GPL12
*/
function recentPosts($atts){
$params = shortcode_atts( array(
'amountOfPosts' => 1,
), $atts );
$getpost = get_posts( array('number' => $params['amountOfPosts']) );
$return = '';
foreach ($getpost as $post) {
$return .= "<div style="font-size: x-large" id=\"". $post->ID ."\"><h1 class=\"entry-title\">" . $post->post_title . "</h1><div class=\"entry-content\">" . $post->post_excerpt . "</div>";
$return .= "<br /><a style="font-size: large" href='" . get_permalink($post->ID) . "'><em>read more →</em></a></div></br>";
}
return $return;
}
add_shortcode('recentPosts', 'recentPosts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment