Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 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 )
  22. {
  23.     setup_postdata($post); ?>
  24.         <option value="<? echo $post->ID; ?>" <?php
  25.         selected( $instance['page_id'], $post->ID );
  26.         ?>><?php the_title(); ?></option>
  27. <?php
  28. }
  29.   function update($new_instance, $old_instance)
  30.   {
  31.     $instance = $old_instance;
  32.     $instance['page_id'] = $new_instance['page_id'];
  33.     return $instance;
  34.   }
  35.  
  36.   function widget($args, $instance)
  37. {
  38.     $post = get_post( $instance['page_id'] );
  39.     echo $post->post_content; // you should add the common filters here ?>
  40.  
  41.     <div id="%1$s" class="widget %2$s">
  42.     <p class="title"><?php echo the_title(); ?></p>
  43.    
  44.     <?php $attachment_id = get_field('main_image');
  45.     $size = "home-boxes";
  46.     $image = wp_get_attachment_image_src( $attachment_id, $size );
  47.     ?>
  48.                
  49.     <a href="<?php the_permalink(); ?>">
  50.         <img class="headline" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" src="<?php echo $image[0]; ?>" />
  51.     </a>
  52.  
  53.     </div>
  54.  
  55. <?php }
  56.  
  57. }
  58. add_action( 'widgets_init', create_function('', 'return register_widget("RandomPostWidget");') );
  59.  
  60.  
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement