ThisWPWidget RandomPosts FromCategory Random Posts from Category

function widget( $args, $instance ) { extract( $args );

$title = apply_filters('widget_title', empty( $instance['title'] ) ? 'Random Posts' : $instance['title']);

if ($instance['postlink'] == 1) { $before title .= '<a href="'.get category link($instance['cat']).'">';

echo $before_title.$title.$after_title;

<ul> <?php query posts("cat=".$instance['cat']."&showposts=".$instance['showposts']."&orderby=r and");

// the Loop if (have posts()) : while (have posts()) : the post(); ?>

<li><a href="<?php the permalink(); ?>" title="<?php the title attribute(); ?>"><?php the_title(); ?></a> <?php if ($instance['content'] == 'excerpt')

the_excerpt(); elseif ($instance['content'] == 'content') the_content();

</ul> <?php echo $after_widget; wp reset query();

function update( $new_instance, $old_instance ) { $instance = $old_instance;

$instance['title'] = strip_tags($new_instance['title']); $instance['cat'] = $new_instance['cat']; $instance['showposts'] = $new_instance['showposts']; $instance['content'] = $new_instance['content']; $instance['postlink'] = $new_instance['postlink']; return $instance;

function form( $instance ) { //Defaults

$instance = wp_parse_args( (array) $instance, array( 'title' => 'Random Posts', 'cat' => 1, 'showposts' => 1,

<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"

name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" /> </p>

<p><label for="<?php echo $this->get_field_id('cat'); ?>">Show posts from category:</label> <?php wp_dropdown_categories(array('name' => $this->get_field_name('cat'), 'hide_empty'=>0, 'hierarchical'=>1, 'selected'=>$instance['cat'])); ?></label>

<input id="<?php echo $this->get_field_id('postlink'); ?>" name="<?php echo $this->get_field_name('postlink'); ?>"

type="checkbox" <?php checked($instance['postlink'], 1); ?> value="1" /> <label for="<?php echo $this->get_field_id('postlink'); ?>">Link widget title to category archive</label> </p>

<p><label for="<?php echo $this->get_field_id('showposts'); ?>">Number of posts to show:</label>

<input class="widefat" id="<?php echo $this->get_field_id('showposts'); ?>" name="<?php echo $this->get_field_name('showposts'); ?>"

type="text" value="<?php echo $instance['showposts']; ?>" />

<label for="<?php echo $this->get_field_id('content'); ?>">Display:</label> <select id="<?php echo $this->get_field_id('content'); ?>" name="<?php echo $this->get_field_name('content'); ?>" class="postform">

<option value="title"<?php selected( $instance['content'], 'title' ); ?>>Title Only</option>

<option value="excerpt"<?php selected( $instance['content'], 'excerpt' ); ?>>Title and Excerpt</option>

<option value="content"<?php selected( $instance['content'], 'content' ); ?>>Title and Content</option> </select> </p>

This time, I'll go through the functions in order, beginning with the setup function alone in Listing

Listing 8-8. The widget setup function function RandomPostsFromCategory() {

$widget_ops = array('classname' => 'random_from_cat', 'description' => 'random posts from a chosen category');

$this->WP_Widget('RandomPostsFromCategory', 'Random Posts from Category',

In the first function, the names have changed. The classname and description are up to you; just make sure the classname is a valid name for a class in CSS (see

www.w3.org/TR/CSS21/syndata.html#characters for details). When you call $this->WP_Widget, you need to pass it three arguments: the name of this widget (which should match this function's name), its title (for the widget manager screen), and the array of options you created in the previous line.

In the widget function (Listing 8-9), you first check to see if the option to make the title a link has been checked. If it has, you need to add the link markup to the $before_title and $after_title variables. Then you move on to the heart of the widget output, which is in this case a Loop. Just as you did in the previous chapters on themes, you'll use query_posts() to get the posts from the selected category and show the chosen number of posts. In this query, rather than getting the most recent posts, you're ordering them at random. And of course, once you're finished with the widget output, you need to reset the query.

Listing 8-9. The widget function function widget( $args, $instance ) { extract( $args );

$title = apply_filters('widget_title', empty( $instance['title'] ) ? 'Random Posts' : $instance['title']);

$before_title .= '<a href="'.get_category_link($instance['cat']).'">'; $after_title = '</a>'.$after_title;

echo $before_title.$title.$after_title;

<ul> <?php query_posts("cat=".$instance['cat']."&showposts=".$instance['showposts']."&orderby=ran d");

// the Loop if (have_posts()) : while (have_posts()) : the_post(); ?>

<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php if ($instance['content'] == 'excerpt') the_excerpt();

elseif ($instance['content'] == 'content') the_content();

</ul> <?php echo $after_widget; wp_reset_query();

The update function (Listing 8-10) changes very little, except that you've added the new options to the array.

Listing 8-10. The update function function update( $new_instance, $old_instance ) { $instance = $old_instance;

$instance['title'] = strip_tags($new_instance['title']); $instance['cat'] = $new_instance['cat']; $instance['showposts'] = $new_instance['showposts']; $instance['content'] = $new_instance['content']; $instance['postlink'] = $new_instance['postlink']; return $instance;

In the form function (Listing 8-11), you need to add the new options' default values to this array as well. Then it's just a matter of adding form inputs for each of the additional options. For the category, you can make use of WordPress's wp_dropdown_categories() function. In the checkbox and select fields, you might have noticed two interesting functions: checked () and selected(). These allow you to compress the code required to see whether or not the relevant option has already been selected or not. The checkbox code, for example, is much simpler than the alternative, shown in Listing 8-12.

Listing 8-11. Theformfunction function form( $instance ) { //Defaults

$instance = wp_parse_args( (array) $instance, array( 'title' => 'Random Posts', 'cat' => 1, 'showposts' => 1, 'content' => 'title', 'postlink' => 0));

<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"

name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" /> </p>

<p><label for="<?php echo $this->get_field_id('cat'); ?>">Show posts from category:</label> <?php wp_dropdown_categories(array('name' => $this->get_field_name('cat'), 'hide_empty'=>0, 'hierarchical'=>1, 'selected'=>$instance['cat'])); ?></label>

<input id="<?php echo $this->get_field_id('postlink'); ?>" name="<?php echo $this->get_field_name('postlink'); ?>"

type="checkbox" <?php checked($instance['postlink'], 1); ?> value="1" /> <label for="<?php echo $this->get_field_id('postlink'); ?>">Link widget title to category archive</label> </p>

<p><label for="<?php echo $this->get_field_id('showposts'); ?>">Number of posts to show:</label>

<input class="widefat" id="<?php echo $this->get_field_id('showposts'); ?>" name="<?php echo $this->get_field_name('showposts'); ?>"

type="text" value="<?php echo $instance['showposts']; ?>" />

<label for="<?php echo $this->get_field_id('content'); ?>">Display:</label> <select id="<?php echo $this->get_field_id('content'); ?>" name="<?php echo $this->get_field_name('content'); ?>" class="postform">

<option value="title"<?php selected( $instance['content'], 'title' ); ?>>Title Only</option>

<option value="excerpt"<?php selected( $instance['content'], 'excerpt' ); ?>>Title and Excerpt</option>

<option value="content"<?php selected( $instance['content'], 'content' ); ?>>Title and Content</option> </select> </p>

Listing8-12. The alternative to checked(); not recommended

<?php if ($instance['postlink']) { ?> checked="checked" <?php } ?>

That's about all there is to widgets! The widget function could contain almost anything. Just keep in mind that widgets are (usually) outside the Loop, so you'll need to access post data accordingly or create your own Loop (as shown in Listing 8-9).

0 0

Post a comment