Jump to content

Displaying a randomly chosen STATIC link


codergirl

Recommended Posts

Hey there, I just registered because I couldn't figure this out by myself after searching and trying for several hours. I'm sure this will be easy to do for most of you.

 

I need a php script which will randomly chose one link from a list and assign this specific link to a single wordpress post only. The link needs to be chosen only once and be static from then on - the link is NOT supposed to change when refreshing the page.

 

The link list would basically look like this:

 

<a href="http://www.google.com" title="Google">Google</a>

<a href="http://www.noodle.com" title="Noodle">Noodle</a>

<a href="http://www.noodle.com/spaghetti/" title="Spaghetti">Spaghetti</a>

<a href="http://www.fantasyunicornworld.com/genocide/" title="Weird">Nonsense</a>

 

Well, you know what I mean. So it's not merely a list of urls but rather a list of tons of urls with different titles and anchor texts.

 

Can anyone here help me achieve the functionality I just tried to explain?

 

Thanks a lot in advance for any possible help.

Link to comment
Share on other sites

You could base it off the ID of the post, Say randomly assign numbers to each multidementional entry of an array, if the sum of the numbers is xxx, then display a certain array entry. This would work on most posts, although there's a chance of repeating (Not sure if you want to use MySQL to handle it or something homebrewed.)

Link to comment
Share on other sites

Say randomly assign numbers to each multidementional entry of an array, if the sum of the numbers is xxx, then display a certain array entry.

 

Thanks for your reply. I'm not a coder myself, I can only write extremely simple lines of code.

 

I know how to set a link according to the category the post is in or according to certain keywords in the post title. I think I could also base it off the post ID in general, but the problem is that a lot of posts are scheduled and new posts are published on a daily basis. My goal is to automate the whole process, I don't have the time to go through hundreds of posts manually. And I have a long list of urls which I want to use.

 

Your approach might work well but I don't have any clue on how to do this. Is there no simple way of selecting something randomly without making the output dynamic?

Link to comment
Share on other sites

Meh, I was bored. I'm not sure this is the best way to go around this, but it does the job. You could also base the seed on the strlen of the keywords+title or whatever.

 

$random = Array(
1 => '<a href="http://www.foo.com" title="foobar">Foo</a>'       ,
2 => '<a href="http://www.google.com" title="Google">Google</a>' ,
3 => '<a href="http://www.noodle.com" title="Noodle">Noodle</a>' ,
4 => '...'    ,
5 => 'And'    ,
6 => 'Some'   ,
7 => 'More'   ,
8 => 'Random' ,
9 => 'Entries',
10 => 'Here'  , 
11 => 'And'   ,
12 => 'Here' );

//$_GET['id'] 
$postnumber = Array(332, 24, 62, 192, 52, 62, 12);

foreach ($postnumber as $post) {
  $decide = str_split($post);
  $iterator = 0;
  foreach($decide as $add) {
    $iterator += $add;
  }
  
  print "Random: " . $random[$iterator] . '<br/>';
}

Link to comment
Share on other sites

Thanks for the code, I'll test it in a couple of hours to see if everything works. It's 5am around here and I already start to have hallucinations and slight paranoia due to lack of sleep. I'll take a nap first.

 

A plugin using mysql would probably be (on of) the best solution(s). What exactly did you use for your PHPBB blog?

Link to comment
Share on other sites

I still need help, as I don't fully understand the code. I placed th following lines of code in my theme's functions.php to test it:

 

function externalurl_is() {
$random = Array(
1 => '<a href="http://www.foo.com" title="foobar">Foo</a>'       ,
2 => '<a href="http://www.google.com" title="Google">Google</a>' ,
3 => '<a href="http://www.noodle.com" title="Noodle">Noodle</a>' ,
4 => '...'    ,
5 => 'And'    ,
6 => 'Some'   ,
7 => 'More'   ,
8 => 'Random' ,
9 => 'Entries',
10 => 'Here'  , 
11 => 'And'   ,
12 => 'Here' );

//$_GET['id'] 
$postnumber = Array(332, 24, 62, 192, 52, 62, 12);

foreach ($postnumber as $post) {
  $decide = str_split($post);
  $iterator = 0;
  foreach($decide as $add) {
    $iterator += $add;
  }
  
  print "Random: " . $random[$iterator] . '<br/>';
}
}

 

I then simply called the function in my theme's single.php, the file for single posts:

 

<?php externalurl_is(); ?>

 

Now it gives me as many posts as there are numbers specified here:

 

$postnumber = Array(332, 24, 62, 192, 52, 62, 12);

 

Well, and now I'm confused. How I do I make it assign only one randomly chosen link per post now? Do I need to use another variable like page_id? Do I need to edit the line with the array above?

 

I seriously have no clue, I never even added an own function before.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.