I'm using Views 2 on a sports website to pull up the 20 most recent article teasers for the front page. I'm trying to figure out a way to insert an add after every 5 articles returned.
The view style is unformatted, and I have successfully set up a php template to override the style so I can put in some custom code (as I couldn't find any setting in views to do this). However, I don't really know PHP, and I could really use a relevent tutorial or code snippet to help me out.
Can anyone give me a bit of advice as to how I can set up the PHP code that will allow me to count the nodes returned and after every certain number of nodes, insert my ad code? Or is there a way to do this within Views itself that I totally missed?
I used to use multiple views and throw the ad code in the footer of the view, with an offset applied to the number of articles returned for each subsequent view. I can't use this method anymore because I simply have too many ads to get in a page, and the number of views is starting to slow the site down.
Comments
Comment #1
dawehnersee for example this code
Comment #2
Geekish commentedThanks! That works perfectly. Following your code and taking what little I know about PHP operators, I can make the 'if' statement check for several numbers at once, like so:
Now I can avoid stacking multiple offset views together and reduce the strain on my server resources. Thanks again, that really helped me out!
Comment #3
dawehneras you see learning php is worth to do it.
Comment #4
peterjmag commentedSorry to bump this back up, but for those who would like a method that can be scaled to a larger result set, try something like this:
You could also turn that into a function and use it elsewhere:
Comment #5
Geekish commentedActually, I'm very glad you posted. The other method works perfectly fine for a set number of articles on my front page and certainly got me out of a pinch. But then I started thinking about the archives and how it's not really feasible to keep specifying $ids in views where there are hundreds of articles that can be returned. And as I want a sprinkling of ads throughout the archive, I was trying to figure out (with my currently minimum PHP knowledge) how to do this very thing.
I may have been able to work it out eventually as I try to learn PHP, but you are a real time-saver! Thank you for posting this. It works like a charm for the views on my site that use pagers.
(However, I did want to mention that I think you might have an extra set of PHP tags surrounding the main block of code, as I had to remove a pair before the template would load without error.)
Comment #6
peterjmag commentedGlad I could help! And thanks for pointing out the extra tags--they were left over after I screwed up my formatting.
For future visitors, here's the corrected snippet:
Comment #8
hectorplus commentedSubscribing
Comment #9
chrislabeard commentedYou guys are awesome! You just saved my life!
Comment #10
arnaudfischer commentedsubscribing
Comment #11
guvser commentedHow would the code look like in drupal 7?
I want to insert an adslot from the Google Admanager module (http://drupal.org/project/google_admanager) after every 10th result.
Comment #12
dawehnerReally similar to this comment: http://drupal.org/node/536830#comment-1885334
You would have to insert this kind of code in the same template file
Comment #14
jusyjim commentedHas anyone done this with a table view?
I tried using this code without luck:
Comment #15
jasom commentedThis is my solution for custom code after n-th (first) row in Drupal 7 and Drupal 8 views.
Note: In digi world data starts with zero: 0, 1, 2, 3... (not like we are used to 1, 2, 3...) so change "== 0" to "== 4" if you wanna to have your custom code after a 5th row.
Here is step-by-step tutorial for newbies how to achieve this: http://www.jasom.net/how-to-add-custom-code-adsense-after-first-row-in-d...