Show Random Posts in WordPress
Notice
Now I will tell you the ‘Send Random’ link in the menu of this site. Our goal is to show random text from the site: For this, we need some codes. Those who follow the site know, it has not been long since he added that link. I also opened a topic in the forum, that’s how I learned. Let me tell you how to do it without further ado. Open the functions.php file of your WordPress site.
Add the following codes to an appropriate place in the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | add_action( 'init' , 'rastgele_yazi' ); function rastgele_yazi(){ if ( isset( $_GET [ 'rastgele' ]) ){ $random = new WP_Query( 'orderby=rand&showposts=1&cat=-314,-512' ); if ( $random ->have_posts()) { while ( $random ->have_posts()) : $random ->the_post(); $URL = get_permalink(); endwhile ; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <head> <meta http-equiv= "Refresh" content= "0; url=<?php echo $URL; ?>" > </head> <body> </body> </html> <?php } die ; } } |
There is a place in this code that we need to change. In the item cat=-314,-512 in the 5th line, the numbers 314 and 512 are the numbers of the categories that you do not want to show random text. Each category has its own number. You can add the number of the category you do not want to show random text there.
We have an additional link to these random text display codes:
1 | <a href= "<?php echo home_url(); ?>/?rastgele" title= "Rastgele Yazı" >Rastgele Yazı</a> |
You can add this link in the menu, as I did, as a text on the sidebar or as an image on the sidebar. So the rest is up to you. I hope my article was helpful to you. If there is a place you hang out, you can specify it in the comments 🙂