Advertisement
rakeshr

working custom widget code with loop inside

Dec 17th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. error_reporting(E_ALL);
  3. /*
  4. Plugin Name: Featured Resources
  5. Plugin URI: http://wpguru.in
  6. Description: Featured resources widget for WPGuru.in
  7. Author: Rakesh Raja
  8. Version: 1.0
  9. Author URI: http://wpguru.in
  10. */
  11.  
  12. class FeaturedResources extends WP_Widget
  13. {
  14.   function FeaturedResources()
  15.   {
  16.     $widget_ops = array('classname' => 'fearesources', 'description' => 'Featured Resources' );
  17.     $this->WP_Widget('FeaturedResources', 'Featured Resources', $widget_ops);
  18.   }
  19.  
  20.   function form($instance)
  21.   {
  22.     $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  23.     $title = $instance['title'];
  24. ?>
  25.   <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
  26. <?php
  27.   }
  28.  
  29.   function update($new_instance, $old_instance)
  30.   {
  31.     $instance = $old_instance;
  32.     $instance['title'] = $new_instance['title'];
  33.     return $instance;
  34.   }
  35.  
  36.   function widget($args, $instance)
  37.   {
  38.     extract($args, EXTR_SKIP);
  39.  
  40.     echo $before_widget;
  41.     $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  42.  
  43.     if (!empty($title))
  44.       echo $before_title . $title . $after_title;;
  45.  
  46.     // WIDGET CODE GOES HERE ?>
  47.    
  48.     <!--resource post-->
  49. <div class="sposts">
  50. <?php
  51. $args = array( 'post_type' => 'resource');
  52. $loop = new WP_Query( $args );
  53. while ( $loop->have_posts()) : $loop->the_post();
  54. if (get_field('featured') == "yes" ) {
  55. echo '<a href="';
  56. the_permalink();
  57. echo '"><div class="spost"><h2>';
  58. the_title();
  59. echo '</h2><div class="left"><img src="';
  60. the_field('thumb');
  61. echo'" alt=""></div><div class="right">';
  62. echo content('100');
  63. echo'<span>Click here to go to read more... </span></div>';
  64. echo'<div style="clear:both;"></div></div></a>';
  65. }
  66. endwhile;
  67. ?>
  68. </div>
  69. <!--resource post-->
  70.  
  71. <?php     echo $after_widget;
  72.   }
  73.  
  74. }
  75. add_action( 'widgets_init', create_function('', 'return register_widget("FeaturedResources");') );?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement