gpt4 book ai didi

wordpress - 在 wordpress 的最后一段之前放置广告

转载 作者:行者123 更新时间:2023-12-04 16:14:58 26 4
gpt4 key购买 nike

这是在第 1、2、3 段之后放置广告的代码。但我想在我的 wordpress 帖子的最后一段之前放置广告。有没有办法做到这一点?

<?php

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, 1, $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 );
}
?>

最佳答案

add_filter('the_content', 'ad_signal');
function ad_signal($content)
{
if (!is_single()) return $content;
$tent = get_the_content();
$content = explode("</p>", $content);
$ss = count($content);
$ns = $ss-1; //**before last paragraph in wordpress **
$new_content = '';
for ($i = 0; $i < count($content); $i++) {
if ($i == $ns ) {

$new_content.= ' <div style="text-align:center;">';
$new_content.= '<h1> Your ads Here ! </h1>';
$new_content.= '</div>';
}


$new_content.= $content[$i] . "</p>";
}

return $new_content;
}

关于wordpress - 在 wordpress 的最后一段之前放置广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com