Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created July 22, 2011 17:11
Show Gist options
  • Save ChrisLTD/1099880 to your computer and use it in GitHub Desktop.
Save ChrisLTD/1099880 to your computer and use it in GitHub Desktop.
Create Wordpress widget that outputs shortcode
<?
function sidebarbrief_func($atts) {
extract(shortcode_atts(array('foo' => 'something'), $atts));
global $more; // Declare global $more (before the loop).
$posts_query = new WP_Query( array( 'post_type' => array('brief'), 'tag_id' => 16, posts_per_page => 1 ) );
$output = '<h1>News in Brief</h1>';
while ( $posts_query->have_posts() ) : $posts_query->the_post();
$more = 0; // Set (inside the loop) to display content above the more tag.
$output .= '<div class="brief">';
$output .= apply_filters('the_content', get_the_content(''));
$output .= ' <a href="' . get_permalink() . '" class="read_more">Read more briefs &rarr;</a>';
$output .= '</div>';
endwhile;
wp_reset_postdata(); // Reset Post Data
return $output;
}
add_shortcode('sidebarbrief', 'sidebarbrief_func');
add_action( 'widgets_init', 'load_theme_widgets' );
function load_theme_widgets() {
register_widget( 'news_brief_widget' );
}
class news_brief_widget extends WP_Widget {
function news_brief_widget() {
$widget_ops = array( 'classname' => 'news_in_brief', 'description' => __('Display news briefs') );
$control_ops = array( 'width' => 300, 'height' => 350 );
$this->WP_Widget( 'product_selector', __('News in Brief'), $widget_ops, $control_ops );
}
/* output */
function widget( $args, $instance ) {
extract( $args );
echo do_shortcode("[sidebarbrief]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment