The function below is used to show certain content after the first paragraph. I would like to show 'content X' after the 1st paragraph and 'content Y' after 2nd paragraph.
<?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>";
} ?>
I appreciate any help.
The function below is used to show certain content after the first paragraph. I would like to show 'content X' after the 1st paragraph and 'content Y' after 2nd paragraph.
<?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>";
} ?>
I appreciate any help.
Share Improve this question edited Nov 28, 2012 at 21:05 BobGCA2 asked Nov 28, 2012 at 18:52 BobGCA2BobGCA2 3171 gold badge6 silver badges17 bronze badges4 Answers
Reset to default 9My way to do this (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>";
}
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 );
}
I knew it's old question but this answer should help the people still looking for it.
This plugin works perfectly in any theme.
https://wordpress/plugins/insert-post-ads
You can choose first / second or whatever you like to display your ads.
I was looking for a way to make calls get_template and here I share it in case it is useful
<?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>";
}
?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744937755a4602130.html
评论列表(0条)