在文章中随机的一个段落后添加一个JS广告,对于很多网站内容是比较合理的。也是更容易让用户浏览到广告的做法。如果你的网站是用php写的程序,可以看看实现随机位置广告的方法。
//$str是你的文章 // 将文章按段落切分 $paragraphs = explode("\n", $str); // 随机选择一个段落 $randomIndex = array_rand($paragraphs); $randomParagraph = $paragraphs[$randomIndex]; // 在随机段落后面插入JS广告 $advert = '<script type="text/javascript">/* 您的广告代码 */</script>'; $randomParagraph .= $advert; // 替换原始段落 $paragraphs[$randomIndex] = $randomParagraph; // 重新将段落拼接为文章 $newStr = implode("\n", $paragraphs); // 输出新的文章内容 echo $newStr;
在上述代码中,首先通过`explode()`函数将文章分割为多个段落,并使用`array_rand()`函数随机选择一个段落。然后,将JS广告代码保存在变量`$advert`中,将其添加到随机选中的段落后面。最后,使用`implode()`函数将修改后的段落重新拼接成新的文章内容。请注意,您需要将您的JS广告代码替换到示例代码中的`/* 您的广告代码 */`处。此外,在实际使用中,您可能需要根据自己的需求进行进一步的调整和优化。
如果你使用zblog_php程序做的网站,那么也可以使用同样的办法,只需要修改模板就行了。下面写一个zblog文章随机段落添加广告的方法。
第一步:找到你的zblog模板文件,一般是post-single.php这个文件。
第二部:修改代码,如下:
{php} $content = $article->Content; // 将文章按段落切分 $paragraphs = explode("\n", $content); // 随机选择一个段落 $randomIndex = array_rand($paragraphs); $randomParagraph = $paragraphs[$randomIndex]; // 在随机段落后面插入JS广告 $advert = '<script type="text/javascript">/* 您的广告代码 */</script>'; $randomParagraph .= $advert; // 替换原始段落 $paragraphs[$randomIndex] = $randomParagraph; // 重新将段落拼接为文章 $content = implode("\n", $paragraphs); // 输出新的文章内容 {/php}
这段代码必须放在模板中的{$article.Content}之前。否则没有效果的哦。