Always Forward

Show Random Posts in WordPress

Notice

This post was auto-translated with Google Translate, and I will proofread it in the upcoming weeks.
This post is over 5 years old, and may not reflect the author’s current view.

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>

rastgele yazı gösterme WordPresste Rastgele Yazı Gösterme
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 🙂

Kerem Mete

Hi, I am a seasoned SEO specialist with 10 years of experience driving organic growth and developing effective SEO strategies. Currently, I am working as a Sr. SEO Manager in Digitas in the UK. For more details, check out my about me page.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button