Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Plugin Name: Category List with Posts Widget
- Plugin URI:
- Description: Category List with Posts and Widget Options
- Author: alchymyth
- Version: 1.a 09/2010
- Author URI: http://www.transformationpowertools.com
- */
- /* Add our function to the widgets_init hook. */
- add_action( 'widgets_init', 'cat_posts_list_load_widgets' );
- /* Function that registers our widget. */
- function cat_posts_list_load_widgets() { register_widget( 'cat_posts_list_Widget' ); }
- class cat_posts_list_Widget extends WP_Widget {
- function cat_posts_list_Widget() {
- /* Widget settings. */
- $widget_ops = array( 'classname' => 'widget_categories cat_posts_list', 'description' => 'Category List with a Number of Posts and Widget Options.' );
- /* Widget control settings. */
- $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'cat_posts_list-widget' );
- /* Create the widget. */
- $this->WP_Widget( 'cat_posts_list-widget', 'Category Posts List', $widget_ops, $control_ops );
- }
- function widget( $args, $instance ) {
- extract( $args );
- /* User-selected settings. */
- $title = apply_filters('widget_title', $instance['title'] );
- $shownumbers = isset( $instance['shownumbers'] ) ? $instance['shownumbers'] : true;
- $numberposts = empty( $instance['numberposts'] ) ? '' : $instance['numberposts'];
- /* Before widget (defined by themes). */
- echo $before_widget;
- /* Title of widget (before and after defined by themes). */
- if ( $title )
- echo $before_title . $title . $after_title;
- /* the actual function. */
- echo '<ul>';
- // get all site categories;
- $categories = get_categories();
- foreach($categories as $category) {
- echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
- if($shownumbers) echo ' (' . $category->category_count . ')'; //show category count
- echo '<ul>';
- // get set number of posts per category;
- $posts = get_posts('category_name='.$category->name.'&numberposts='.$numberposts);
- if($posts) :
- foreach($posts as $post) {
- setup_postdata($post);
- echo '<li>';
- echo '<a href="' . get_permalink($post->ID) . '" title="link to ' . get_the_title($post->ID) . '">' . get_the_title($post->ID) . '</a>';
- echo '</li>';
- }
- endif;
- echo '</ul></li>';
- } //end 'foreach($categories as $category)'
- echo '</ul>';
- wp_reset_query();
- /* end of the actual function */
- /* After widget (defined by themes). */
- echo $after_widget;
- }
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- /* Strip tags (if needed) and update the widget settings. */
- $instance['title'] = strip_tags( $new_instance['title'] );
- $instance['shownumbers'] = $new_instance['shownumbers'] ? 1 : 0;
- $instance['numberposts'] = strip_tags( $new_instance['numberposts'] );
- return $instance;
- }
- function form( $instance ) {
- //Defaults
- $instance = wp_parse_args( (array) $instance, array( 'shownumbers' => false, 'title' => '', 'numberposts' => '5') );
- $title = esc_attr( $instance['title'] );
- $numberposts = esc_attr( $instance['numberposts'] );
- ?>
- <p>
- <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
- <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('numberposts'); ?>"><?php _e( 'Max Posts per Category:' ); ?></label> <input type="text" value="<?php echo $numberposts; ?>" name="<?php echo $this->get_field_name('numberposts'); ?>" id="<?php echo $this->get_field_id('numberposts'); ?>" class="widefat" />
- <br />
- </p>
- <p>
- <input class="checkbox" type="checkbox" <?php checked( $instance['shownumbers'], true ); ?> id="<?php echo $this->get_field_id( 'shownumbers' ); ?>" name="<?php echo $this->get_field_name( 'shownumbers' ); ?>" />
- <label for="<?php echo $this->get_field_id( 'shownumbers' ); ?>">Show Post Count for Category?</label>
- </p>
- <?php
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement