以下代码需要粘贴到您的子主题或特定于站点的插件的文件中。functions.php
让我们从第一个位置开始,然后在帖子的开头插入广告或其他内容。
帖子的开头
要将广告或任何其他内容插入文章的开头(第一段之前),请使用以下代码:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$beforePostContent = '<p>Content at the beginning of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $index == 0 ) {
$paragraphs[$index] = $beforePostContent . $paragraph;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
帖子结束
要将广告或任何其他内容插入到文章末尾(最后一段之后),请使用以下代码:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$afterPostContent = '<p>Content at the end of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( count($paragraphs) == $index + 1 ) {
$paragraphs[$index] .= $afterPostContent;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
帖子中间
要在文章的中间插入广告或任何其他内容,请使用以下代码:
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
// contet to add number 1
$insertContent_1 = '<p>Content in the middle of the post</p>';
$position_1 = floor(count($paragraphs) / 2);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $position_1 == $index + 1 ) {
$paragraphs[$index] .= $insertContent_1;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );
更多常见位置
要将广告或任何其他内容插入到以下位置,请使用帖子中间模板(上面的模板),并将第 7 行 () 替换为下面列表中的相关位置。$position_1 = floor(count($paragraphs) / 2)
在帖子的前三分之一之后
$position_1 = floor(count($paragraphs) / 3);
在帖子的第二个三分之一之后
$position_1 = floor(count($paragraphs) / 1.5);
帖子的第一段之后
$position_1 = 1;
帖子的第二段之后
$position_1 = 2;
帖子结束前一段
$position_1 = floor(count($paragraphs) - 2);
文章结尾前两段
$position_1 = floor(count($paragraphs) - 3);
插入到帖子的多个区域
你们中的许多人希望将上面的一些位置结合起来,并在帖子的多个位置插入广告或其他内容。
例如,帖子开头的广告和帖子中间的图片。
为此,我创建了下面的模板,并试图为非开发人员使其尽可能简单易行。
下面的模板将在帖子的开头、中间和结尾插入内容。如果你不想在那里添加任何内容,你可以保留或留空(就像这样:)。$beforePostContent
$afterPostContent
$beforePostContent = '';
请根据您的需要随意使用帖子中间的位置。您可以使用我上面提到的代码片段。此外,如果您愿意,您可以向代码添加更多位置。例如,帖子的前三分之一和后三分之一的内容(您也可以观看视频进行澄清)。
function pfwp_insert_content_into_post($content){
if (is_singular('post')) {
$beforePostContent = '<p>Content at the beginning of the post</p>';
$afterPostContent = '<p>Content at the end of the post</p>';
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
// Contet number 1
$insertContent_1 = '<p>Mid post content goes here</p>';
$position_1 = floor(count($paragraphs) / 2);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $index == 0 ) {
$paragraphs[$index] = $beforePostContent . $paragraph;
}
// Add content number 1 to anywhere in the post
if ( $position_1 == $index + 1 ) {
$paragraphs[$index] .= $insertContent_1;
}
if ( count($paragraphs) == $index + 1 ) {
$paragraphs[$index] .= $afterPostContent;
}
}
$content = implode( '', $paragraphs );
}
return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );