Tagged: combo widget
-
AuthorPosts
-
February 3, 2015 at 6:01 am #389845
Hi,
Currently, the combo widget tracks the most popular posts, most recent posts, and most recent comments across the all the posts in my database.
1. Is it possible to limit the Popular tab to only track posts published within the last seven days?
2. Is it possible to exclude posts from certain categories from this widget? Certain tags?Thank you.
February 3, 2015 at 9:05 am #389891Hi!
Copy the following code to your child theme functions.php:
class avia_combo_widget extends WP_Widget { function avia_combo_widget() { //Constructor $widget_ops = array('classname' => 'avia_combo_widget', 'description' => 'A widget that displays your popular posts, recent posts, recent comments and a tagcloud' ); $this->WP_Widget( 'avia_combo_widget', THEMENAME.' Combo Widget', $widget_ops ); } function widget($args, $instance) { // prints the widget extract($args, EXTR_SKIP); $posts = empty($instance['count']) ? 4 : $instance['count']; echo $before_widget; echo "<div class='tabcontainer border_tabs top_tab tab_initial_open tab_initial_open__1'>"; echo '<div class="tab first_tab active_tab widget_tab_popular"><span>'.__('Popular', 'avia_framework').'</span></div>'; echo "<div class='tab_content active_tab_content'>"; avia_get_post_list( array('cat'=>'', 'orderby'=> 'comment_count','posts_per_page' => $posts, 'date_query' => array( array( 'after'=>'1 week ago') ) ) ); echo "</div>"; echo '<div class="tab widget_tab_recent"><span>'.__('Recent', 'avia_framework').'</span></div>'; echo "<div class='tab_content'>"; avia_get_post_list( array('cat'=>'', 'orderby'=> 'post_date','showposts' => $posts, 'date_query' => array( array( 'after'=>'1 week ago') ) ) ); echo "</div>"; echo '<div class="tab widget_tab_comments"><span>'.__('Comments', 'avia_framework').'</span></div>'; echo "<div class='tab_content'>"; avia_get_comment_list( array('number' => $posts, 'status' => 'approve', 'order' => 'DESC', 'date_query' => array( array( 'after'=>'1 week ago') ) ) ); echo "</div>"; echo '<div class="tab last_tab widget_tab_tags"><span>'.__('Tags', 'avia_framework').'</span></div>'; echo "<div class='tab_content tagcloud'>"; wp_tag_cloud('smallest=12&largest=12&unit=px'); echo "</div>"; echo "</div>"; echo $after_widget; } function update($new_instance, $old_instance) { $instance = $old_instance; foreach($new_instance as $key=>$value) { $instance[$key] = strip_tags($new_instance[$key]); } return $instance; } function form($instance) { //widgetform in backend $instance = wp_parse_args( (array) $instance, array('count' => 4) ); if(!is_numeric($instance['count'])) $instance['count'] = 4; ?> <p> <label for="<?php echo $this->get_field_id('count'); ?>">Number of posts you want to display: <input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($instance['count']); ?>" /></label></p> <?php } }
That will override the default combo widget class with a custom one. I’ve already modified it to query only posts from the last seven days, but you’d need to set the categories you don’t want in the query, look for the two
'cat'=>''
in the code and fill it this way:cat=-12,-34,-56
12, 34, 56 would be the categories IDs you want to exclude.
Regards,
JosueFebruary 10, 2015 at 6:34 am #393495Thanks so much for the customization, worked like a charm. Thank you!!
February 10, 2015 at 7:42 am #393526You are welcome, glad to help :)
Regards,
Josue -
AuthorPosts
- The topic ‘Limiting the Combo Widget to the last seven days?’ is closed to new replies.