在特定类别的帖子中显示广告

时间:2017-04-03 作者:Azeez

我想在特定类别的帖子中显示广告。我还想在另一个类别中显示广告,不仅是一个类别,而且每次我再次插入代码并更改类别id时,都会导致“白屏”错误。你能告诉我如何扩展这个功能,我将能够在特定类别的不同帖子中显示不同的广告吗?

下面是我用来显示广告的功能:

add_filter(\'the_content\', \'wpse_ad_content\');

function wpse_ad_content($content)
{
    if (!is_single()) return $content;
    if(!in_category(\'7\')) return $content;
    $paragraphAfter = 2; //Enter number of paragraphs to display ad after.
    $content = explode("</p>", $content);
    $new_content = \'\';
    for ($i = 0; $i < count($content); $i++) {
        if ($i == $paragraphAfter) {
            $new_content.= \'<div style="width: 300px; height: 250px; padding: 6px 6px 6px 0; float: left; margin-left: 0; margin-right: 18px;">\';
            $new_content.= \'//Enter your ad code here....\';
            $new_content.= \'</div>\';
        }

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

2 个回复
SO网友:Jignesh Patel

这段代码把你的广告放在哪里。

<div>
    <?php 
        global $post;
        $terms = get_the_terms( $post->ID, \'post\' );
            foreach ($terms as $term) {
                $product_cat_id = $term->term_id;
                    break;
                }
            if($product_cat_id==\'18\'){
                // your ads code put here for this categories
            }else if($product_cat_id==\'19\'){
                // your ads code put here for this categories
            }
            else{
                // your default ads when categories not found then it display.
            }
    ?>
</div>

MY Updated code :

function wpse_ad_content($content)
{global $post;
    if (!is_single()) return $content;
    if(!in_category(\'13\')) return $content;
    $paragraphAfter = 2; //Enter number of paragraphs to display ad after.
    $content = explode("</p>", $content);
    $new_content = \'\';
    for ($i = 0; $i < count($content); $i++) {
        if ($i == $paragraphAfter) {
            $new_content.= \'<div style="width: 300px; height: 250px; padding: 6px 6px 6px 0; float: left; margin-left: 0; margin-right: 18px;">\';

                $terms = get_the_terms( $post->ID, \'category\' );
                foreach ($terms as $term) {
                    $product_cat_id = $term->term_id;
                        break;
                    }

                if($product_cat_id==\'13\'){

                    $new_content.= \'aaaaa\';
                }else if($product_cat_id==\'19\'){
                    // your ads code put here for this categories
                    $new_content.= \'bbbb\';
                }
                else{
                    // your default ads when categories not found then it display.
                    $new_content.= \'cccc\';
                }
                $new_content.= \'</div>\';
        }

        $new_content.= $content[$i] . "</p>";
    }
    return $new_content;
}
注意:更改类别id。

SO网友:Johansson

您不能两次使用此代码,因为它正在声明一个函数,并且两次使用相同名称声明一个函数将触发致命错误。如果要扩展该函数,可以通过以下方式使用:

if(!(in_category(\'7\') OR in_category(\'8\') OR in_category(\'9\'))) return $content;
这将显示这三个类别的广告。