Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a view that output the content nodes as grid. (instigram style). How to insert an advertisement block in a random location within the views list.

Node Node . . Advertisement . . Node Node

share|improve this question
    
I think you must use the views hooks after generating result. then count the result's rows and select the random position between rows , finally you must load your advertisement block and insert it into the #markup nodes in the selected rows. For this issue, module programming is needed buy maybe the other friends can help you to do it only with views definition. –  Mehrdad201 Feb 11 at 7:37

2 Answers 2

up vote 6 down vote accepted

You can override the format specific template like views-view-unformatted.tpl.php (you can find the right file based on your requirement) and then use some custom code like

  <?php if (($id % 2) == 0): ?>
    Do what-ever you want to do here. 
  <?php endif; ?>

This module hosted on github https://github.com/pedroposada/custom_views_nthrow seems to do the same more modularly.

Instructions : http://www.pedroposada.com/blog/insert-whatever-every-nth-row-views.html-0

share|improve this answer
1  
Thx !! Just what i needed !! i ended overriding "views-view-unformatted.tpl.php" and inserting the ad block in a random location wihin the loop. –  Barjas Feb 11 at 8:19

If you want to have your custom code after first row then override your default views-view-unformatted.tpl.php with this code:

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
  <?php if ($id == 0 || $id == 5): ?>
Your custom adsense code
  <?php endif; ?>
<?php endforeach; ?>

Step-by-step tutorial for newbies on how to override default views-view-unformatted.tpl.php and achieve custom code after for example first and sixth row is here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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