Hi there...

I'm new with php... and I'm trying to figure out where to edit to get a span arround the primary links.
I tryed looking at the page.tpl.php file, but that's not the place I guess...

Could anyone give me a hint??

Comments

ludo1960’s picture

Page.tpl is the right place to do it!

bjste’s picture

Could you explain how?

Caus the things I've tryed just results in errors...
I assume it's this section that schould be edited:

if (isset($primary_links)) :
print theme('links', $primary_links, array('class' => 'links primary-links'))
endif;

It is clear that it's a Menuitem I need.. and not a span arround the whole menu, right?
I need to use the span to place a piece of bagground-image.

roper.’s picture

template.php is what you want.

put this in there:

function THEMENAME_links($links, $attributes = array('class' => 'links')) {
  global $language;
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = $key;

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
          && (empty($link['language']) || $link['language']->language == $language->language)) {
        $class .= ' active';
      }
      $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $link['html'] = TRUE;
        $output .= l('<span>' . $link['title'] . '</span>', $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}

What that does is override theme_links(), simply by wrapping the link title with a span on these lines:

$link['html'] = TRUE;
$output .= l('<span>' . $link['title'] . '</span>', $link['href'], $link);

So that should do it, granted I didn't test that exact snippet... You'll need to clear the theme registry at /admin/settings/performance.

:-)

bjste’s picture

I can't get this to work... I think I need a little more instruction on how to do this...

It seems like a lot of code to need to get a little span tag?
Do I have to remove the excisting 3 lines of code in page.tpl.php?
Should I edit anything in this code or should it just work?

roper.’s picture

Here's a quick rundown...

The theme_links() function is part of Drupal core and it determines what is outputted when you use theme('links', $primary_links, array('class' => 'links primary-links')); in your page.tpl.php file (or wherever).

Now, any function that starts with theme_whatever() can be overridden by your site's theme in template.php. In order to do that, you need to copy the entire original function, make your changes, and put that in template.php. You MUST rename the function from "theme_whatever()" to "yourthemename_whatever()" in template.php though.

After that, go to http://example.com/admin/settings/performance and click "Clear cached data" so the override will be found.

The stuff you have in page.tpl.php in your previous post should stay there. And the snippet I posted in my last post you can copy/past (it already has the change in it), but you need to just rename the function to yourthemename_links

bjste’s picture

Thank you so much for your help! I really appreciate it! It works perfect now :D

roper.’s picture

You're welcome, glad you got it working. :-)

zakir.gori’s picture

I was converting theme from html template to Drupal theme from last 3 dayes i was searching same thing now i got. thanks again you Rock man.

zakir.gori