[Plugin: Flexible Recent Posts] [patch] Don't repeat posts
-
I created a patch that prevents repeating the same post on the same page. I’m using this on my home page as I have 4 instances of the widget in different sidebars (the way the template is laid out), and I don’t want repeats.
In my case, I have one widget showing the “latest” post, then the other 3 show the latest from specific categories. With this patch, if the newest happens to be in category A, then the category A column will show the second newest. If I have a post that happens to be in multiple categories, it too will not be repeated.
It’s a bit crude because it’s using global variables — but that is the WordPress way I guess, and echos what the plugin already does. ??
I did not make a UI to enable this behaviour, though I certainly could if this patch will be accepted.
Index: class-recent-posts-widget.php =================================================================== --- class-recent-posts-widget.php (revision 580132) +++ class-recent-posts-widget.php (working copy) @@ -66,6 +66,10 @@ $instance['terms'] = apply_filters( 'frp_terms', $instance['terms'] ); $instance['all_posts_title'] = htmlspecialchars( $instance['all_posts_title'] ); + // exclude posts we've already shown + $instance['dont_repeat'] = true; // TODO: make a configuration setting + $exclude_posts = ($instance['dont_repeat'] && is_array($GLOBALS['frp_posted_ids'])) ? $GLOBALS['frp_posted_ids'] : array(); + if ( !empty( $instance['terms'] ) || !empty( $instance['all_terms'] ) ) { $tax = key( empty( $instance['terms'] ) ? $instance['all_terms'] : $instance['terms'] ); $terms = ( empty( $instance['all_terms'] ) ) ? current( $instance['terms'] ) : get_terms( $tax, array( 'fields' => 'ids' ) ); @@ -73,6 +77,7 @@ $query = array( 'numberposts' => $instance['number'], 'post_type' => 'any', + 'post__not_in'=> $exclude_posts, 'tax_query' => array( array( 'taxonomy' => $tax, Index: templates/recent-posts-widget.php =================================================================== --- templates/recent-posts-widget.php (revision 580132) +++ templates/recent-posts-widget.php (working copy) @@ -18,6 +18,9 @@ foreach ( $posts as $post ): // Replace global post variable with current looped post. $GLOBALS['post'] = $post; + + // remember that we posted this + $GLOBALS['frp_posted_ids'][] = $post->ID; ?> <li class="frp-news"> <?php echo wpautop( do_shortcode( $instance['template'] ) ); ?>
https://www.ads-software.com/extend/plugins/flexible-recent-posts/
- The topic ‘[Plugin: Flexible Recent Posts] [patch] Don't repeat posts’ is closed to new replies.