0

I want to make a website the changes the title and the Facebook Open Graph description everytime the page loads. I know how to change just the title but not the Open Graph tags because they use HTML characters.

Here is how i will do it with PHP just for the title

<html>
<head>
<title><?php

$titles = array();

$titles[] = "Possible Title 1";
$titles[] = "Possible Title 2";
$titles[] = "Etc. Etc.";

srand ((float) microtime() * 10000000); // Seed the random number generator

echo $titles[array_rand($titles)]; // Pick a random item from the array and output it
?></title>
</head>
<body>
</body>
</html>

This are the tags i want to randomize:

  <meta property="og:title" content="Possible title 1" />
    <meta property="og:image" content="Possible image 1"  />
    <meta property="og:description" content="Possible description 1 "/>
    <title>Possible title 1</title>

1 Answers1

0

This should do the trick:

<?php
$titles = array('title1', 'title2', 'title3');
$images = array('image1', 'image2', 'image3');
$descriptions = array('description1', 'description2', 'description3');
?>
<meta property="og:title" content="<?php echo $titles[array_rand($titles)); ?>" />
<meta property="og:image" content="<?php echo $images[array_rand($images)); ?>"  />
<meta property="og:description" content="<?php echo $descriptions[array_rand($descriptions)); ?>"/>
<title><?php echo $titles[array_rand($titles)); ?></title>
Matt
  • 718
  • 6
  • 20
  • Thanks man. but that gives me everything random. I want it if it shows title1 it will show image1 and description1 too. I was thinking on putting everything on a single array and just print out random arrays – Israel Pelayo May 30 '17 at 23:46
  • If i put all the text on a single array i get: Parse error: syntax error, unexpected ',' i dont know why – Israel Pelayo May 30 '17 at 23:55
  • Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde May 31 '17 at 00:19