How can I modify a post depending on the source of the traffic?

Not a wordpress questions really. You will get much better responses at stackoverflow.com. However you instigated my curiosity… you can get the referral url by using the following javascript: document.referrer You could add that to your current AD script to check if the document.referrer == “some url” before displaying the ad. See more examples here

Advertisement in Loop Posts wordpress

Use WP_Query and then loop the results using break when you want to stop to put the ad. $args = [ ‘posts_per_page’ => 5 ]; $posts = new WP_Query($args); Then loop 2 <section class=”posts”> <?php $cont = 0; ?> <?php while( $posts->have_posts() ) : $posts->the_post(); $cont++; $id = get_the_ID(); //Show your post here if($cont >= … Read more