Advertisement
Guest User

Sydney-Child fp-latest-news

a guest
Mar 5th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. <?php
  2.  
  3. class Sydney_Latest_News extends WP_Widget {
  4.  
  5. function sydney_latest_news() {
  6. $widget_ops = array('classname' => 'sydney_latest_news_widget', 'description' => __( 'Show the latest news from your blog.', 'sydney') );
  7. parent::__construct(false, $name = __('Sydney FP: Latest News', 'sydney'), $widget_ops);
  8. $this->alt_option_name = 'sydney_latest_news_widget';
  9.  
  10. }
  11.  
  12. function form($instance) {
  13. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  14. $category = isset( $instance['category'] ) ? esc_attr( $instance['category'] ) : '';
  15. $see_all_text = isset( $instance['see_all_text'] ) ? esc_html( $instance['see_all_text'] ) : '';
  16. ?>
  17.  
  18. <p>
  19. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'sydney'); ?></label>
  20. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  21. </p>
  22.  
  23. <p><label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Enter the slug for your category or leave empty to show posts from all categories.', 'sydney' ); ?></label>
  24. <input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo $category; ?>" size="3" /></p>
  25.  
  26. <p><label for="<?php echo $this->get_field_id('see_all_text'); ?>"><?php _e('Add the text for the button here if you want to change the default <em>See all our news</em>', 'sydney'); ?></label>
  27. <input class="widefat custom_media_url" id="<?php echo $this->get_field_id( 'see_all_text' ); ?>" name="<?php echo $this->get_field_name( 'see_all_text' ); ?>" type="text" value="<?php echo $see_all_text; ?>" size="3" /></p>
  28.  
  29. <?php
  30. }
  31.  
  32. function update($new_instance, $old_instance) {
  33. $instance = $old_instance;
  34. $instance['title'] = strip_tags($new_instance['title']);
  35. $instance['category'] = strip_tags($new_instance['category']);
  36. $instance['see_all_text'] = strip_tags($new_instance['see_all_text']);
  37.  
  38. $alloptions = wp_cache_get( 'alloptions', 'options' );
  39. if ( isset($alloptions['sydney_latest_news']) )
  40. delete_option('sydney_latest_news');
  41.  
  42. return $instance;
  43. }
  44.  
  45. // display widget
  46. function widget($args, $instance) {
  47. $cache = array();
  48. if ( ! $this->is_preview() ) {
  49. $cache = wp_cache_get( 'sydney_latest_news', 'widget' );
  50. }
  51.  
  52. if ( ! is_array( $cache ) ) {
  53. $cache = array();
  54. }
  55.  
  56. if ( ! isset( $args['widget_id'] ) ) {
  57. $args['widget_id'] = $this->id;
  58. }
  59.  
  60. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  61. echo $cache[ $args['widget_id'] ];
  62. return;
  63. }
  64.  
  65. ob_start();
  66. extract($args);
  67.  
  68. $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
  69. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  70. $category = isset( $instance['category'] ) ? esc_attr($instance['category']) : '';
  71. $see_all_text = isset( $instance['see_all_text'] ) ? esc_html($instance['see_all_text']) : __( 'See all our news', 'sydney' );
  72. if ($see_all_text == '') {
  73. $see_all_text = __( 'See all our news', 'sydney' );
  74. }
  75.  
  76. $r = new WP_Query( array(
  77. 'no_found_rows' => true,
  78. 'post_status' => 'publish',
  79. 'posts_per_page' => 4,
  80. 'category_name' => $category
  81. ) );
  82.  
  83. echo $args['before_widget'];
  84.  
  85. if ($r->have_posts()) :
  86. ?>
  87. <?php if ( $title ) echo $before_title . $title . $after_title; ?>
  88.  
  89. <?php while ( $r->have_posts() ) : $r->the_post(); ?>
  90. <div class="blog-post col-md-3 col-sm-6 col-xs-12">
  91. <?php if ( has_post_thumbnail() ) : ?>
  92. <div class="entry-thumb">
  93. <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
  94. <?php the_post_thumbnail('sydney-medium-thumb'); ?>
  95. </a>
  96. </div>
  97. <?php endif; ?>
  98. <?php the_title( sprintf( '<h4 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h4>' ); ?>
  99. <div class="entry-summary"><?php the_excerpt(); ?></div>
  100. </div>
  101. <?php endwhile; ?>
  102.  
  103. <?php $cat = get_term_by('slug', $category, 'category') ?>
  104. <?php if ($category) : //Link to the category page instead of blog page if a category is selected ?>
  105. <a href="<?php echo esc_url(get_category_link(get_cat_ID($cat -> name))); ?>" class="roll-button more-button"><?php echo $see_all_text; ?></a>
  106. <?php else : ?>
  107. <a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>" class="roll-button more-button"><?php echo $see_all_text; ?></a>
  108. <?php endif; ?>
  109. <?php
  110. echo $args['after_widget'];
  111. wp_reset_postdata();
  112.  
  113. endif;
  114.  
  115. if ( ! $this->is_preview() ) {
  116. $cache[ $args['widget_id'] ] = ob_get_flush();
  117. wp_cache_set( 'sydney_latest_news', $cache, 'widget' );
  118. } else {
  119. ob_end_flush();
  120. }
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement