-
-
Save markjaquith/859c2c588403cb2cacf6 to your computer and use it in GitHub Desktop.
A test shortcode plugin that outputs the most recent posts, while not stomping the current loop.
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
<?php | |
/* | |
Plugin Name: Recent Posts Shortcode Test | |
Author: Mark Jaquith | |
Author URI: http://coveredwebservices.com/ | |
*/ | |
class CWS_Recent_Posts_Shortcode_Test_Plugin { | |
static $instance; | |
private $id; | |
private $post; | |
public function __construct() { | |
self::$instance = $this; | |
add_shortcode( 'recent-posts', array( $this, 'recent_posts_shortcode' ) ); | |
add_shortcode( 'foo', array( $this, 'foo_shortcode' ) ); | |
} | |
public function recent_posts_shortcode() { | |
global $post, $id; | |
// Stash the current state of $post and $id, since we're going to overwrite them | |
$this->post = $post; | |
$this->id = $id; | |
// Most recent 5 posts, with recursion prevention | |
$q = new WP_Query( array( 'posts_per_page' =>5, 'post__not_in' => array( $id ) ) ); | |
$return = ''; | |
while( $q->have_posts() ) : $q->the_post(); | |
$return .= '<hr /><h3>' . get_the_title() . '</h3>' . apply_filters( 'the_content', get_the_content() ); | |
endwhile; | |
// Restore $post and $id | |
$post = $this->post; | |
$id = $this->id; | |
return $return; | |
} | |
public function foo_shortcode() { | |
return "FOO SHORTCODE"; | |
} | |
} | |
// Bootstrap | |
new CWS_Recent_Posts_Shortcode_Test_Plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment