The code you're putting in your page.tpl.php has the right idea, but you're missing a couple of things:
- The body of the foreach loop should be surrounded by the PHP tags, so that PHP will interpret and execute the code. This is why you see that Drupal just "prints out the code": because you're putting it out of the PHP "world", so it simply becomes part of your template's HTML.
- Even if you correctly execute the code, you will not see any changes, because by default, the
$link
variable in your foreach loops is a copy of the original item in the array, so doing $link['href'] = 'stuff'
won't modify the original. To modify the original, you can use the reference syntax, like: foreach ($primary_links as &$link)
. (Ampersand prefixed to the variable name, see PHP docs on references).
- And finally, even if you fix the previous two issues, it might still not work because the link's HREF attribute is probably going to be processed by theme('links') later, and your "?" and "=" are going to get encoded and it will break the link.
So, fixing those three issues, I'd say you should modify your page.tpl.php code to look like:
<?php if (is_array($primary_links)) : ?>
<?php foreach ($primary_links as &$link): ?>
<?php $link['query'] = array('device' => 'mobile'); ?>
<?php endforeach; ?>
<?php endif; ?>
Or, if it annoys you to have to open/close the PHP on every line, just use a normal block like:
<?php
if (is_array($primary_links)) {
foreach ($primary_links as &$link) {
$link['query'] = array('device' => 'mobile');
}
}
?>
Note 1. The &$link
syntax (use reference instead of copy), and 2. The query array key of the $link
array, which is one of those "special" array keys that Drupal will search for, and, if found, utilize to build a proper URL query to attach to the final link (see the docs for Drupal's url() function).
Also, remember to clear the caches whenever you see that "nothing changes", especially when working on a theme.