将广告放在纯文字段落之间

Deb*_*Das 6 php wordpress

我使用以下代码在我的内容中放置一些广告代码.

<?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, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>
Run Code Online (Sandbox Code Playgroud)

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

我想避免使用jQuery.

我的用例示例...... 我的用例示例

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

Pie*_*sen 6

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

快速响彻

  • 我没有删除第一段中的附件.我真的不明白为什么内容必须为广告而被破坏

  • 我在代码中做了很多检查,以确保广告仅插入文本段落之间.仅文本段落被视为不包含img,li和ul标记的段落

  • 我已经正确记录了每个代码块,因此您可以轻松地完成每个部分.todo如有必要,我还添加了您需要注意的文档块

  • 我曾经习惯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 <http://stackoverflow.com/users/1908141/pieter-goosen>
     *  @return       Ads in between $content
     *  @link         http://stackoverflow.com/q/25888630/1908141
     * 
     *  Special thanks to the following answers on my questions that helped me to
     *  to achieve this
     *     - http://stackoverflow.com/a/26032282/1908141
     *     - http://stackoverflow.com/a/25988355/1908141
     *     - http://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, 0, $midpoint );
            if( $count%2 == 1 ) {
                $second = array_slice( $paragraphs, $midpoint, $midpoint, true );
            }else{
                $second = array_slice( $paragraphs, $midpoint, $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;
}
Run Code Online (Sandbox Code Playgroud)

编辑

从你的评论:

  • 您应该使用以下代码进行调试<pre><?php var_dump($NAME_OF_VARIABLE); ?></pre>.对于您的第一个问题,我将?><pre><?php var_dump($paragraphs); ?></pre><?php在此行之后使用,$paragraphs = 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标签

  • 注意:使用 `&lt;/p&gt;` 进行分解会创建没有 `&lt;/p&gt;` 的输出。所以我正在使用 `$paragraphs = preg_split("/(?=&lt;\/p&gt;)/", $content, null, PREG_SPLIT_DELIM_CAPTURE);` 这似乎工作正常。 (2认同)