如何在wordpress帖子的第一句后放置广告?

时间:2011-05-24 21:28:20

标签: wordpress code-injection advertisement

我想知道在WP帖子的第一句话或第二句话之后使用什么技术来放置广告。

实际上可能会有更多的变化和麻烦(帖子本身使用的HTML标签,在我们之前执行的其他插件插入的代码)

我不确定简单地使用PHP爆炸功能是否安全(在句子末尾加点)

或者可能已经为此创建了插件?

1 个答案:

答案 0 :(得分:0)

这是一个快速而肮脏的答案,效果出奇的好。

<?php
    $ad       = '<b>!!!ADVERTISEMENT!!!</b>';
    $content = get_the_content();
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);

    //Matches a word with a space before it and a period after it.
    $array        = preg_split('/\s(\w+){1}\.(?!\w)/', $content, 2, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
    $end              = array_pop($array);
    $delimiterMatch  = array_pop($array);
    $delimiterMatch .= '. ';//the period disappeared and im too lazy to find out why.
    $beginning    = array_pop($array);

    echo $beginning . $delimiterMatch . $ad . $end;

?>

随意乱用正则表达式来获得你想要的分裂。