在第一段和第二段之后显示内容

时间:2012-11-28 作者:BobGCA

下面的函数用于显示第一段之后的某些内容。我想在第一段后显示“内容X”,在第二段后显示“内容Y”。

<?php
$paragraphAfter= 1; //display after the first paragraph
$content = apply_filters(\'the_content\', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter) { ?>

<div>Insert content here</div>

<?php }
echo $content[$i] . "</p>";
} ?>
我感谢你的帮助。

4 个回复
最合适的回答,由SO网友:Max Yudin 整理而成

我的方法(see update below):

function addParagraphs($content) {
    // you can add as many as you want:
    $additions = array(
        \'<p>After 1st paragraph</p>\',
        \'<p>After 2nd paragraph</p>\'
    );

    $content = get_the_content();

    $output = \'\'; // define variable to avoid PHP warnings

    $parts = explode("</p>", $content);

    $count = count($parts); // call count() only once, it\'s faster

    for($i=0; $i<$count; $i++) {
        $output .= $parts[$i] . \'</p>\' . $additions[$i]; // non-existent additions does not concatenate
    }
    return $output;

}
add_filter(\'the_content\',\'addParagraphs\');
Answer is updated according to subsequent comments:

$paragraphAfter[1] = \'<div>AFTER FIRST</div>\'; //display after the first paragraph
$paragraphAfter[3] = \'<div>AFTER THIRD</div>\'; //display after the third paragraph
$paragraphAfter[5] = \'<div>AFTER FIFtH</div>\'; //display after the fifth paragraph

$content = apply_filters( \'the_content\', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
    if ( array_key_exists($i, $paragraphAfter) ) {
        echo $paragraphAfter[$i];
    }
    echo $content[$i] . "</p>";
}

SO网友:Karra
add_filter( \'the_content\', \'prefix_insert_post_ads\' );

function prefix_insert_post_ads( $content ) {

    $ad_code = \'<div>Ads code goes here</div>\';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = \'</p>\';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }

    return implode( \'\', $paragraphs );
}
SO网友:Ramkumar M

我知道这是个老问题,但这个答案应该能帮助人们继续寻找它。

这个插件在任何主题中都能完美工作。

https://wordpress.org/plugins/insert-post-ads

你可以选择第一/第二或任何你喜欢的方式来显示你的广告。

SO网友:joseyaz

我一直在寻找一种调用get\\u模板的方法,我在这里分享它,以防有用

<?php 
            $paragraphAfter[1] = "get_template_part( \'part-related\', \'ad-first\' );";
            $paragraphAfter[3] = "get_template_part( \'part-related\', \'ad-third\' );"; //display after the fifth paragraph
            $paragraphAfter[5] = "get_template_part( \'part-related\', \'ad-fifth\' );";


            $content = apply_filters( \'the_content\', get_the_content() );
            $content = explode("</p>", $content);
            $count = count($content);
            for ($i = 0; $i < $count; $i++ ) {
                if ( array_key_exists($i, $paragraphAfter) ) {
                $string = eval($paragraphAfter[$i]); // Eval string
                    echo $string;
                }
                echo $content[$i] . "</p>";
            }
     ?>

结束

相关推荐

如何在POST_CONTENT中的350个单词之后插入一个短标签?

我有700篇帖子,大部分都超过1000字。我正在将此设置为会员网站。我正在使用wishlist成员,并希望保护前350个单词之后的所有内容。这需要在第350个单词后放置一个短标签,并在文章的最后放置一个短标签。在末尾添加短代码似乎非常简单。然而,我不知道如何将短代码放到post\\u内容的中间。。我考虑了几种不同的方法,包括:a、 尝试在dreamweaver中使用xml平面文件B找到并替换正则表达式。尝试在mysql中使用查询来查询帖子内容字段,计算该字段中的单词数,然后插入shortcode标记the