每次页面加载时如何显示不同的Open Graph标题?

时间:2017-05-30 23:02:23

标签: php

我希望每次页面加载时都会更改网站标题和Facebook Open Graph描述。我知道如何更改标题而不是Open Graph标记,因为它们使用HTML字符。

以下是我将如何使用PHP进行标题

<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>

这是我要随机化的标签:

  <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 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

<?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>