我使用以下代码在我的内容中放置一些广告代码.
<?PHP
$content = apply_filters('the_content',$post->post_content);
$content = explode (' ',$content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ',array_slice($content,$halfway_mark));
$second_half_content = implode(' ',$halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>

如何修改此设置,以便我可以在仅文本段落之间同时放置2个广告(包含广告的< p> …< / p>不应包含图片或嵌入的视频).

我想避免使用jQuery.

我的用例示例……

>我想在本文中插入2个广告块.
>我希望第一个广告块在第一段之后.但是应删除第1段中的任何图像.
>对于第二个广告,它应该放在文章的后半部分中,在纯文本段落之间,这样广告代码在大量文本之间切换,并且永远不会非常接近嵌入的图像或视频等.
> 2个广告之间至少应有2个段落

这是我对这个问题的处理方法.很抱歉发布有点迟了(并且错过了赏金:-(),但这是一个忙碌的一周,所以我做了一切都是点点滴滴.

快速响彻

>我没有删除第一段中的附件.我真的不明白为什么内容必须为广告而被破坏
>我在代码中做了很多检查,试图确保广告仅插入文本段落之间.仅文本段落被视为不包含img,li和ul标记的段落
>我已正确记录每个代码块,因此您可以轻松完成每个部分.如果有必要,我还添加了你需要注意的todo doc块
>我使用wptexturize将p标签应用于the_content.每个双折线构成一个段落
>根据需要修改并使用此代码.我已经对它进行了测试,因此我身上没有错误.

这是代码.希望这对你有用. * PS!所有这些都进入你的functions.PHP.您的模板文件不需要任何其他代码或mod

<?PHP
add_filter( 'the_content','so_25888630_ad_between_paragraphs' );

function so_25888630_ad_between_paragraphs($content){
    /**-----------------------------------------------------------------------------
     *
     *  @author       Pieter Goosen <https://stackoverflow.com/users/1908141/pieter-goosen>
     *  @return       Ads in between $content
     *  @link         https://stackoverflow.com/q/25888630/1908141
     * 
     *  Special thanks to the following answers on my questions that helped me to
     *  to achieve this
     *     - https://stackoverflow.com/a/26032282/1908141
     *     - https://stackoverflow.com/a/25988355/1908141
     *     - https://stackoverflow.com/a/26010955/1908141
     *     - http://wordpress.stackexchange.com/a/162787/31545
     *
    *------------------------------------------------------------------------------*/ 
    if( in_the_loop() ){ //Simply make sure that these changes effect the main query only

        /**-----------------------------------------------------------------------------
         *
         *  wptexturize is applied to the $content. This inserts p tags that will help to  
         *  split the text into paragraphs. The text is split into paragraphs after each
         *  closing p tag. Remember,each double break constitutes a paragraph.
         *  
         *  @todo If you really need to delete the attachments in paragraph one,you want
         *        to do it here before you start your foreach loop
         *
        *------------------------------------------------------------------------------*/ 
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p,wptexturize($content) );

        /**-----------------------------------------------------------------------------
         *
         *  The amount of paragraphs is counted to determine add frequency. If there are
         *  less than four paragraphs,only one ad will be placed. If the paragraph count
         *  is more than 4,the text is split into two sections,$first and $second according
         *  to the midpoint of the text. $totals will either contain the full text (if 
         *  paragraph count is less than 4) or an array of the two separate sections of
         *  text
         *
         *  @todo Set paragraph count to suite your needs
         *
        *------------------------------------------------------------------------------*/ 
        $count = count( $paragraphs );
        if( 4 >= $count ) {
            $totals = array( $paragraphs ); 
        }else{
            $midpoint = floor($count / 2);
            $first = array_slice($paragraphs,$midpoint );
            if( $count%2 == 1 ) {
                $second = array_slice( $paragraphs,$midpoint,true );
            }else{
                $second = array_slice( $paragraphs,$midpoint-1,true );
            }
            $totals = array( $first,$second );
        }

        $new_paras = array();   
        foreach ( $totals as $key_total=>$total ) {
            /**-----------------------------------------------------------------------------
             *
             *  This is where all the important stuff happens
             *  The first thing that is done is a work count on every paragraph
             *  Each paragraph is is also checked if the following tags,a,li and ul exists
             *  If any of the above tags are found or the text count is less than 10,0 is 
             *  returned for this paragraph. ($p will hold these values for later checking)
             *  If none of the above conditions are true,1 will be returned. 1 will represent
             *  paragraphs that qualify for add insertion,and these will determine where an ad 
             *  will go
             *  returned for this paragraph. ($p will hold these values for later checking)
             *
             *  @todo You can delete or add rules here to your liking
             *
            *------------------------------------------------------------------------------*/ 
            $p = array();
            foreach ( $total as $key_paras=>$paragraph ) {
                $word_count = count(explode(' ',$paragraph));
                if( preg_match( '~<(?:img|ul|li)[ >]~',$paragraph ) || $word_count < 10 ) {  
                    $p[$key_paras] = 0; 
                }else{
                    $p[$key_paras] = 1; 
                }   
            }

            /**-----------------------------------------------------------------------------
             *
             *  Return a position where an add will be inserted
             *  This code checks if there are two adjacent 1's,and then return the second key
             *  The ad will be inserted between these keys
             *  If there are no two adjacent 1's,"no_ad" is returned into array $m
             *  This means that no ad will be inserted in that section
             *
            *------------------------------------------------------------------------------*/ 
            $m = array();
            foreach ( $p as $key=>$value ) {
                if( 1 === $value && array_key_exists( $key-1,$p ) && $p[$key] === $p[$key-1] && !$m){
                    $m[] = $key;
                }elseif( !array_key_exists( $key+1,$p ) && !$m ) {
                    $m[] = 'no-ad';
                }
            } 

            /**-----------------------------------------------------------------------------
             *
             *  Use two different ads,one for each section
             *  Only ad1 is displayed if there is less than 4 paragraphs
             *
             *  @todo Replace "PLACE YOUR ADD NO 1 HERE" with your add or code. Leave p tags
             *  @todo I will try to insert widgets here to make it dynamic
             *
            *------------------------------------------------------------------------------*/ 
            if( $key_total == 0 ){
                $ad = array( 'ad1' => '<p>PLACE YOUR ADD NO 1 HERE</p>' );
            }else{
                $ad = array( 'ad2' => '<p>PLACE YOUR ADD NO 2 HERE</p>' );
            }

            /**-----------------------------------------------------------------------------
             *
             *  This code loops through all the paragraphs and checks each key against $mail
             *  and $key_para
             *  Each paragraph is returned to an array called $new_paras. $new_paras will
             *  hold the new content that will be passed to $content.
             *  If a key matches the value of $m (which holds the array key of the position
             *  where an ad should be inserted) an add is inserted. If $m holds a value of
             *  'no_ad',no ad will be inserted
             *
            *------------------------------------------------------------------------------*/ 
            foreach ( $total as $key_para=>$para ) {
                if( !in_array( 'no_ad',$m ) && $key_para === $m[0] ){
                    $new_paras[key($ad)] = $ad[key($ad)];
                    $new_paras[$key_para] = $para;
                }else{
                    $new_paras[$key_para] = $para;
                }
            }
        }

        /**-----------------------------------------------------------------------------
         *
         *  $content should be a string,not an array. $new_paras is an array,which will
         *  not work. $new_paras are converted to a string with implode,and then passed
         *  to $content which will be our new content
         *
        *------------------------------------------------------------------------------*/ 
        $content =  implode( ' ',$new_paras );
    }
    return $content;
}

编辑

从你的评论:

>您应该使用以下代码来调试< pre><?PHP var_dump($NAME_OF_VARIABLE); ?>< /预取代.对于您的第一个问题,我将使用?>< pre><?PHP var_dump($paragraph); ?>< / pre><?PHP就在这行$paragraph = explode($closing_p,wpautop($content));准确地看看内容是如何分开的.这将让您了解您的内容是否正确分割.
>还使用?>< pre><?PHP var_dump($p); ?>< / pre><?PHP就在此行之后$p = array();检查特定段落的值.请记住,带有img,li和ul标签的段落应该有0,而且段落少于10个单词.其余的应该是1
>编辑 – >解决了剥离段落的问题.谢谢你指出这个漏洞给我.从未意识到这一点我已经更新了代码,所以只需复制并粘贴它即可

编辑2

请注意,您无法使用所使用的在线工具测试代码.这并不是the_content()输出内容的真实反映.您使用在线工具看到的输出将被过滤并标记,然后作为the_content()发送到屏幕.如果您使用Google Chrome等浏览器检查输出,则会看到正确应用了p标记.

我还在代码中更改了带有image标签的a标签

php – 在仅文本段落之间放置广告的更多相关文章

  1. AmazeUI 折叠面板的实现代码

    这篇文章主要介绍了AmazeUI 折叠面板的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. 有关HTML5页面在iPhoneX适配问题

    这篇文章主要介绍了有关HTML5页面在iPhoneX适配问题,需要的朋友可以参考下

  3. Html5百叶窗效果的示例代码

    本篇文章主要介绍了Html5百叶窗效果的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. html5中使用hotcss.js实现手机端自适配的方法

    这篇文章主要介绍了html5中使用hotcss.js实现手机端自适配的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 从iOS应用程序发送帖子到PHP脚本不工作…简单的解决方案就像

    我之前已经做了好几次了但是由于某些原因我无法通过这个帖子…我尝试了设置为_POST且没有的变量的PHP脚本……当它们未设置为发布时它工作精细.这是我的iOS代码:这里是PHP的一大块,POST变量不在正确的位置?我想这对于更有经验的开发人员来说是一个相当简单的答案,感谢您的帮助!解决方法$_POST是一个数组,而不是一个函数.您需要使用方括号来访问数组索引:

  6. IOS 10.2简单警报和徽章上的UserNotifications问题

    徽章问题我也试过在我的应用程序图标上实现了一个很好的标记,直到我试图删除它.这是执行它的功能:现在它没有增加徽章编号,因为它的名称应该建议,但它只是将徽章编号设置为1.文档说如果你将content.badge设置为0,它会删除它,但这不起作用.我尝试使用其他数字,当我手动将其更改为“2”,“3”等时……

  7. swift QQ聊天界面 cell 的计算

    ;/***是否隐藏时间*/varhideTime:Bool?.font=UIFont;//设置文本距离UIButton的边距_content.contentEdgeInsets=UIEdgeInsetsMake//填充背景if(chatMessageModel?.sourceId==7){//7是本人,暂时先写成硬编码bgImage.image=UIImage;}else{bgImage.image=UIImage;}//算内容所占的空间if(chatMessageModel?.hideTime==1){

  8. swift学习2 元组 tuples

    swift中出现了一种新的数据结构,非常牛掰的元组tuples如果懂PHP的猿,会发现这个元组和PHP的数组非常类似,同样是可以默认不指定key,也可以指定key目前的学习疑问是,如何进行元组的遍历?

  9. Swift学习笔记2网络数据交换格式XML,JSON解析 [iOS实战 入门与提高卷]

    示例文档Notes.xml创建XMLParser类调用与运行结果用TBXML来解析XML文档TBXML是第三方框架,使用起来比NSXML更简单。

  10. 尝试使用swift mailer,gmail smtp,php发送邮件

    这里是我的代码:在运行时出现此错误…

随机推荐

  1. PHP个人网站架设连环讲(一)

    先下一个OmnihttpdProffesinalV2.06,装上就有PHP4beta3可以用了。PHP4给我们带来一个简单的方法,就是使用SESSION(会话)级变量。但是如果不是PHP4又该怎么办?我们可以假设某人在15分钟以内对你的网页的请求都不属于一个新的人次,这样你可以做个计数的过程存在INC里,在每一个页面引用,访客第一次进入时将访问时间送到cookie里。以后每个页面被访问时都检查cookie上次访问时间值。

  2. PHP函数学习之PHP函数点评

    PHP函数使用说明,应用举例,精简点评,希望对您学习php有所帮助

  3. ecshop2.7.3 在php5.4下的各种错误问题处理

    将方法内的函数,分拆为2个部分。这个和gd库没有一点关系,是ecshop程序的问题。会出现这种问题,不外乎就是当前会员的session或者程序对cookie的处理存在漏洞。进过本地测试,includes\modules\integrates\ecshop.php这个整合自身会员的类中没有重写integrate.php中的check_cookie()方法导致,验证cookie时返回的username为空,丢失了登录状态,在ecshop.php中重写了此方法就可以了。把他加到ecshop.php的最后面去就可

  4. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  5. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  6. zen_cart实现支付前生成订单的方法

    这篇文章主要介绍了zen_cart实现支付前生成订单的方法,结合实例形式详细分析了zen_cart支付前生成订单的具体步骤与相关实现技巧,需要的朋友可以参考下

  7. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 基于win2003虚拟机中apache服务器的访问

    下面小编就为大家带来一篇基于win2003虚拟机中apache服务器的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. Yii2中组件的注册与创建方法

    这篇文章主要介绍了Yii2之组件的注册与创建的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

返回
顶部