How to display random link on page refresh

dekadent30

Elite Member
Joined
Nov 10, 2008
Messages
1,910
Reaction score
1,036
I need to display random link from list (in txt file) on my wp blog's sidebar on every page refresh . How to do that in PHP? Or maybe you know any plugin? List of urls will be big - 5k-10k urls
 
You could write all the links in a text file, one per line.
Then use the function to read text files in PHP, and display the value of a random line with
Code:
rand(1, 5000);

Hope this helps! I have no time to write up the code atm
 
PHP:
<?php $f_contents = file("list.txt"); $line = $f_contents[array_rand ($f_contents)]; echo $line; ?>

should do the work, it's just missing a bit of formatting.
There are few plugins that could help you - but would prolly slow down the site.
Test the code above please, and let me know how/if it works, and we can work from there.
 
I tried this code but it displays my blog url instead of random link. Where i should upload list.txt file? To the same folder where sidebar.php (of specific theme) is placed? Or to the main wordpress folder with index file?
 
I tried this code but it displays my blog url instead of random link. Where i should upload list.txt file? To the same folder where sidebar.php (of specific theme) is placed? Or to the main wordpress folder with index file?
In wp-content/uploads
 
I wrote php code in sidebar.php (link should display on sidebar of blog) so should i write full path to wp-content/upload/2021/list.txt ?
 
Back
Top