在第一段之后添加一个元素

时间:2017-03-10 07:02:09

标签: php wordpress

我在WordPress网站的帖子the_content之前添加了以下代码。

<?php if (!empty($smof_data['ads_entry_top'])) { ?>
   <div class="entry-img-300"><?php echo stripslashes($smof_data['ads_entry_top']); ?></div>
<?php } ?>

我希望在第一段之后添加它,所以将此函数写入functions.php

add_filter( 'the_content', 'insert_after_first', 20 );

function insert_after_first( $content ) {

    $content = preg_replace( "/<\/p>/", "</p>" . stripslashes($smof_data['ads_entry_top']) , $content, 1 );
    return $content;
}

但它什么都没做。有什么问题?

1 个答案:

答案 0 :(得分:0)

我改变了我的方法,现在在第一段之后添加一些内容,并带有以下功能和过滤器。我在这里添加它可能会帮助某人:

add_filter('the_content', 'ata_the_content_filter', 10, 1);
function ata_the_content_filter($content)
{
    $parags = explode('</p>', $content);
    $parags[0] .= '<br>hello';// add whatever you want after first paragraph

    $content_new = '';

    foreach ($parags as $parag) {
        $content_new .= $parag;
    }

    return $content_new;
}