0

I'm using the Views Grid layout and I'm looking for a way to place Google Adsense ads randomly within the grid. Here is the code in views-view-grid.tpl.php:

<?php if (!empty($title)) : ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<table class="<?php print $class; ?>"<?php print $attributes; ?>>
  <?php if (!empty($caption)) : ?>
    <caption><?php print $caption; ?></caption>
  <?php endif; ?>

  <tbody>
    <?php foreach ($rows as $row_number => $columns): ?>
      <tr <?php if ($row_classes[$row_number]) { print 'class="' . $row_classes[$row_number] .'"';  } ?>>
        <?php foreach ($columns as $column_number => $item): ?>
          <td <?php if ($column_classes[$row_number][$column_number]) { print 'class="' . $column_classes[$row_number][$column_number] .'"';  } ?>>
            <?php print $item; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

Is editing the template file the correct way to do this? I'm not a programmer so a lot of this is beyond me but I am willing to learn.

2 Answers 2

1

Nice attempt, but no the template file is not the best place for that.

I would work on including the ads in your View result rather than trying to hack the template file.

Does your view show rendered content? Easy, make a new content type that contains your ads and include that content type in your filter.

Does your view show fields? Same thing, just make sure the ad content type has the same fields as the content you're showing in the grid.

2
  • Is there a way to make the ads show up randomly on the page using this method? From what I tried using your method, it allows ads to show up on the view, but through the Radioactivity module I'm using, they end up moving down the page. Is there a way to prevent that so that they just show up randomly?
    – Sam
    Commented Jan 15, 2015 at 3:58
  • Hm... that would require some special sorting... Looks like J. Reynolds has chimed in with some stuff I wasn't fully aware of.
    – Darvanen
    Commented Jan 15, 2015 at 4:30
0

The template is the correct place to print it. However, you should just print a pre-populated variable in the template, and actually populate the variable in a preprocess function for the template.

First, you need to rename your views-view-grid.tpl.php by using an appropriate suggestion, unless of course you want your code to run on every view of grid format.

Your preprocess function will be named HOOK_preprocess_views_view_grid. The same preprocess function is used for all suggestions of the template. In this preprocess function you will need to test the view object's name and display_id so that it runs for your specific view (unless of course you want it to run on all views of format grid). Then you need to add a variable containing a single/array of Google Adsense scripts/html you want to include.

You then print the already populated variable in your template (if it is an array you will have to print individual items).

Install devel module and use dpm statements in your code to help you drill down into the structures.

Another possible way to add your adds into the output of a view is hook_views_pre_render, or hook_views_post_render if you are prepared to do some search and replace on the assembled views string. This last option might actually be the easiest as you will get all occurrences of a pattern (</tr><tr), select a few randomly, then replace it (</tr><tr><td colspan="X">adcode</td></tr><tr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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