• Hello,

    I use a custom code to display ads on my site. Basically, it’s a block with a few text ads in it that gets inserted after the nth paragraph of each post.

    Is there any way to rotate the ads? What I mean is, when visitors click and visit another post/page, I’d like for a different set of ads to be displayed.

    So that way a visitor doesn’t see the same ads no matter what page/post he or she visits.

    I am hoping that would help with “ad blindness” and increase conversions and revenue.

    Here’s the current code that I am using to display the ad:

    // Lets Insert first custom ad after nth paragraph of each post
    add_filter( 'the_content', 'prefix_insert_first_ad' );
    function prefix_insert_first_ad( $content ) {
    	$ad_code = '<div>
    <div class="custom-ad">
    
    ----MY CUSTOM AD GOES HERE----
    
    </div>';
    if ( is_single() && ! is_admin() ) {
    		return prefix_insert_after_paragraph( $ad_code, 6, $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 );
    }

    Thank you very much in advance.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    This would involve being able to keep track of which ads were served, and informing the next page what they were. If the ads each had an associated ID, that would make tracking much simpler. They could all be declared in a PHP array and their index into the array could be their ID. Or each ad could even be a custom post type. All posts always have their own unique ID.

    There are 3 basic ways to pass data between requests. The ads already served could be saved in a cookie. Or their IDs could be passed as an URL query string, for example example.com/my-page/?ads=1,5,11,7. Or the IDs could be saved in PHP session variables. Each has pros and cons. The simplest is probably an URL query string.

Viewing 1 replies (of 1 total)
  • The topic ‘How to rotate 2 different custom ads’ is closed to new replies.