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

时间:2019-01-24 作者:joseyaz

您已使用此问题的代码(Show content after the first and second paragraph) 这对我来说是正确的。

    <?php
$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>";
} ?>
我一直在观看和混音。但我还没有找到添加调用类型“get\\u template\\u part”的方法。我无法识别get\\u template\\u part的部分。哪里有错误,还是做不到?

更改此项:

$paragraphAfter[1] = \'<div>AFTER FIRST</div>\'; 
为此:

$paragraphAfter[1] = \'<div> get_template_part( \'ad-first\' );</div>\'; 
我试着做出一个简单的回应,但有些东西我失败了

$paragraphAfter[1] = \'<div> echo "Hello world!"; </div>\'; 
我不能让它工作:-(欢迎任何帮助或指导,谢谢

3 个回复
SO网友:joseyaz

我找到了一种调用get\\u template\\u部件的方法。

<?php 
$paragraphAfter[1] = \'<div><?php get_template_part( "part-related", "ad-first" ); ?></div>\'; //display after the first paragraph
$paragraphAfter[3] = \'<div><?php get_template_part( "part-related", "ad-third" ); ?></div>\'; //display after the third paragraph
$paragraphAfter[5] = \'<div><?php get_template_part( "part-related", "ad-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) ) {
      $string = eval(\'?>\'.$paragraphAfter[$i].\'<?php;\');    //  CLOSE PHP SINCE THE CHAIN ​​TO EVALUATE OPENS IT, AND THEN, OPEN PHP AS THE CHAIN ​​TO EVALUATE IT CLOSES IT
        echo $string;
    }
    echo $content[$i] . "</p>";
    }
?>

SO网友:joseyaz

我认为这是前一个版本的改进版本:

<?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>";
            }
     ?>

SO网友:Emerson Thompson
function insert_custom_content($content) {
    $custom_content = \'<div>Content to be inserted</div>\';

    if (is_single() && ! is_admin()) {
        return insert_after_paragraph($custom_content, 2, $content);
    }

    return $content;
}
add_filter(\'the_content\', \'insert_custom_content\');

function 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);
}