Skip to content

Instantly share code, notes, and snippets.

@tracend
Created March 4, 2012 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracend/1969952 to your computer and use it in GitHub Desktop.
Save tracend/1969952 to your computer and use it in GitHub Desktop.
WP Coda Slider: Coda Slider featured category slider with shortcodes
<?php //Header
/*
Plugin Name: WP Coda Slider
Plugin URI: http://wp-performance.com/wp-coda-slider/
Description: Coda Slider featured category slider with shortcodes
Author: c3mdigital, makesites
Author URI: http://c3mdigital.com/
Version: 0.2.6
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
// You can use the short code or template tag if you need to parse posts that include a shortcode
// To use the template tag add (in PHP): if( function_exists('c3m_wpcodaslider') ) { c3m_wpcodaslider($id, $cat, $show, $args);}
// you must supply the variables when you add the function to your template. Example: c3m_codaslider('myslider', '81', '4', 'dynamicArrows:false');
// this would add a slider with the id of myslider and show 4 posts from category 81 with dynamic arrows set to false.
// all the variables must be present and in the same order.
// initiate the wpcodaslider class, containing the bulk of the plugin logic
$my_wpcodaslider = new wpcodaslider();
// load the JavaScript dependencies
add_action ('init', 'c3m_coda_scripts');
// Main plugin class
class wpcodaslider{
var $shortcode_name = 'wpcodaslider';
var $pattern = '<!-- wpcodaslider -->';
var $posts_content = '';
function wpcodaslider() {
add_shortcode( $this->shortcode_name, array( &$this, 'shortcode' ) );
return $this;
}
// insert the shortcode in any page ie: [wpcodaslider id=slidername cat=4 show=3] will show first three post in category with id $
function shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'cat' => null,
'id' => null,
'show' => null,
'args' => null
), $atts ) );
//Make sure there is a query and name
if (! $cat || ! $id)
return 'Could not load slider. Mallformed shortcode.';
$o = $this->markup( $id, $cat, $show, $args );
return $o;
}
// the function that is used by both the shortcode and the php method to create the markup
function markup( $id, $cat, $show, $args ){
// form the query
$query = array(
'post_type' => 'post',
'cat' => $cat,
'posts_per_page' => $show
);
// get the posts
$posts = get_posts( $query);
// start creating the markup
$o = '
<div class="coda-slider-wrapper">
<div class="coda-slider preload" id="'. $id .'">';
foreach($posts as $post){
$o.=
'<div class="panel" id="post-' . $post->ID . '">
<div class="panel-wrapper">
<h2 class="title">' . $post->post_title . '</h2>
' . apply_filters('the_content', $post->post_content) . '
</div> <!-- .panel-wrapper -->
</div><!-- .panel -->';
}
$o.='
</div><!-- .coda-slider .preload -->
</div><!-- coda-slider-wrapper -->
<script type="text/javascript">
jQuery(document).ready(function($){
$().ready(function() {
$(\'#'. $id .'\').codaSlider({' . $args .'});
});
});
</script>';
return $o;
}
}
// Helper Functions
// - Template tag method
function c3m_wpcodaslider($id, $cat, $show, $args) {
global $my_wpcodaslider;
echo $my_wpcodaslider->markup( $id, $cat, $show, $args );
return $id;
return $cat;
return $show;
return $args;
}
// - JavaScipt loader
function c3m_coda_scripts() {
wp_enqueue_script('coda_slider', WP_PLUGIN_URL . '/wp-coda-slider/js/coda.slider.js', array( 'jquery' ));
wp_enqueue_style('coda_slider_css', WP_PLUGIN_URL . '/wp-coda-slider/css/coda-slider-2.0.1.css');
wp_localize_script( 'coda_slider', 'Plugin_Url', array( 'plugin_url' => WP_PLUGIN_URL ) );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment