0

I wanted to display ads on in post in a particular category. I also want to display ads in another category not only one but every time I insert the code again and change the category id, it causes a "white screen" error. Can you tell me how can I extend that function, that I will be able to display different ads in different post in a particular category?

Below is the function that i use to display the ad:

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;
}
  • The link you provided is not valid. Can you please copy your code here to see what's wrong with it? – Jack Johansson Apr 3 '17 at 6:02
0

You can not use this code twice, since it's declaring a function, and declaring a function twice with the same name will trigger a fatal error. If you want to extend the function, you can use it this way:

if(!(in_category('7') OR in_category('8') OR in_category('9'))) return $content;

This will show the ads on these 3 categories.

  • Thank you all for your reply,but please is it not possible to display ads based on a particular category at after a particular paragraph. For example, display a different ads on seo category, different ads on wordpress category..... something like. – Azeez Apr 3 '17 at 11:08
0

This code put where when you display your ads.

<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;
}

NOTE : Change your categories id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.