Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class RandomPostWidget extends WP_Widget
- {
- function RandomPostWidget()
- {
- $widget_ops = array('classname' => 'RandomPostWidget', 'description' => 'Displays a featured post' );
- $this->WP_Widget('RandomPostWidget', 'Featured Homepage Post', $widget_ops);
- }
- function form($instance)
- {
- $instance = wp_parse_args( (array) $instance, array( 'page_id' => '' ) );
- $title = $instance['page_id'];
- ?>
- <select label="<?php echo $this->get_field_id('title'); ?>">>
- <?php
- global $post;
- $args = array( 'numberposts' => -1,'category' => 5);
- $posts = get_posts($args);
- foreach( $posts as $post )
- {
- setup_postdata($post); ?>
- <option value="<? echo $post->ID; ?>" <?php
- selected( $instance['page_id'], $post->ID );
- ?>><?php the_title(); ?></option>
- <?php
- }
- function update($new_instance, $old_instance)
- {
- $instance = $old_instance;
- $instance['page_id'] = $new_instance['page_id'];
- return $instance;
- }
- function widget($args, $instance)
- {
- $post = get_post( $instance['page_id'] );
- echo $post->post_content; // you should add the common filters here ?>
- <div id="%1$s" class="widget %2$s">
- <p class="title"><?php echo the_title(); ?></p>
- <?php $attachment_id = get_field('main_image');
- $size = "home-boxes";
- $image = wp_get_attachment_image_src( $attachment_id, $size );
- ?>
- <a href="<?php the_permalink(); ?>">
- <img class="headline" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" src="<?php echo $image[0]; ?>" />
- </a>
- </div>
- <?php }
- }
- add_action( 'widgets_init', create_function('', 'return register_widget("RandomPostWidget");') );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement