So far in my website ads appears only in desktop screens.
I wonder how to show ads even in small screens, with a minimum change of php code.
I.g. I’m wondering if it could be possible to add an ads content (with a php variable) before every h2 tag of a webpage.
I saw this script, but it works clicking a button: how get it working opening a page?
EDIT
I tried unsuccessfully this:
//adding ads in small screen
var phpads = "<?php echo "<div id='pubsmall' class='slideshow-container' role='complementary'>$ads_vertical</div>\n"; ?>";
$(window).on('load', function() {
$("h2").prepend("phpads");
});
EDIT
Something like this works on a single attempt-page, but not in my website
<?php
$adsmall = "<div id='pubsmall' class='slideshow-container' role='complementary'>$ads_verticale_libromese</div>";
?>
<script>
//adding ads in small screen
var phpads = "<?php echo "$adsmall"; ?>";
$(window).on('load', function() {
$("h2").prepend(phpads);
});
</script>
But now I wonder if it would be a good idea to put automatically an ad before everyh2…
In some pages it could fit, but others, with many h2 it would be too heavy…
Maybe I should add a css nth: to set only the second and the fourth h2.
I found the problem (a carousel script). Now, with a “simple” variable php I have a code working.
But I should fix a grafic problem: the div added (via jquery) is within the h2 tag… (quite ugly)
EDIT
Fixed also this problem, using .before instead of .prepend
Hi @web148, mixing JS and PHP logic is notoriously error prone and hard to reason about. Maybe consider rendering your ad markup in template elements, and then use JS for your actual alternating logic… something like this:
Uhm… if I understand, to use your code I should put an html element (template) in every place of my webpages where I want to show the ads: shouldn’t I?
But in this way I would have a lot of code to add, while I was seaching a way to automatize with the less code possible the ads in small screens.
My question could be very simple: how should I modify that code to get not a randomized succession (suite), but an ordered one (that is first $banner1, then $banner2, and so on)?
… but not in my desired mode: that is, all the ads are the same (in a given moment) and are changed (all together) every refresh.
While I want that any ads be different from others in the same time.
What I want (maybe I’m wrong from from a marketing point of view, I don’t sure) is a php (or css) code that put a different ads in different places of a webpage.
Just once per page and ad. It’s a simple wrapper around your ad markup and shouldn’t be much overhead really.
You’re assigning the first element of the PHP array to a JS variable here, and then adding that very same element to the DOM over and over. In order to loop over the ads, you need a JS collection of those ads, which could be achieved using templates as shown above.
Edit: Not exactly best practice but kinda okay, another way would be to dump the array of ads as JSON like so: