Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. class RandomPostWidget extends WP_Widget
  4. {
  5.   function RandomPostWidget()
  6.   {
  7.     $widget_ops = array('classname' => 'RandomPostWidget', 'description' => 'Displays a featured post' );
  8.     $this->WP_Widget('RandomPostWidget', 'Featured Homepage Post', $widget_ops);
  9.   }
  10.  
  11.   function form($instance)
  12. {
  13.     $instance = wp_parse_args( (array) $instance, array( 'page_id' => '' ) );
  14.     $title = $instance['page_id'];
  15. ?>
  16.     <select label="<?php echo $this->get_field_id('title'); ?>">>
  17.         <?php
  18.         global $post;
  19.         $args = array( 'numberposts' => -1,'category' => 5);   
  20.         $posts = get_posts($args);
  21.         foreach( $posts as $post ) : setup_postdata($post); ?>
  22.        
  23.         <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
  24.         <?php endforeach; ?>
  25.     </select>
  26.  
  27. <?php
  28.   }
  29.  
  30.   function update($new_instance, $old_instance)
  31.   {
  32.     $instance = $old_instance;
  33.     $instance['page_id'] = $new_instance['page_id'];
  34.     return $instance;
  35.   }
  36.  
  37.   function widget($args, $instance)
  38. {
  39.     $post = get_post( $instance['page_id'] );
  40.     echo $post->post_content; // you should add the common filters here ?>
  41.  
  42.     <p class="title"><?php echo the_title(); ?></p>
  43.  
  44.  
  45. <?php }
  46.  
  47. }
  48. add_action( 'widgets_init', create_function('', 'return register_widget("RandomPostWidget");') );
  49.  
  50.  
  51. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement