excluding more than one category from random post
-
I found a couple of random post plugins for the sidebar but they all call for using widgets and come with their own styling that seems like too much work to customize.
I decided to use the coding recommended in the codex: https://codex.www.ads-software.com/Template_Tags/get_posts#Random_posts
But I had a little difficulty figuring out how to exclude more than one category. After googling, I came across the following page
https://blog.websitestyle.com/index.php/2006/11/02/wordpress-how-to-exclude-categories-from-a-feed/ to finally come up with this:$args = array( 'posts_per_page' => 5, 'orderby' => 'rand', 'category' => -4, 'category' => -7, 'category' => -13 );
It works, but there must be a shorter way to exclude more than one category! Please advise.
Thank you.
E Morris
This is the full coding I have right now:
<!-- start random post links --> <li><strong><?php _e('5 randomly chosen posts:'); ?></strong></li> <?php $args = array( 'posts_per_page' => 5, 'orderby' => 'rand', 'category' => -4, 'category' => -7, 'category' => -13 ); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> <!-- end random post links -->
- The topic ‘excluding more than one category from random post’ is closed to new replies.