Aktivem Menüpunkt eine Klasse zuweisen
Eingetragen von furri (14)
am 22.07.2008 - 09:29 Uhr in
am 22.07.2008 - 09:29 Uhr in
Hallo
ich möchte dem aktiven Menüpunkt eine Klasse "active" zuweisen, so wie hier beschrieben. Leider funktioniert das bei mir nicht, ich verwende als Basis meines Themes das Zen-Theme und finde nun in meiner page.tpl.php Datei die theme_links Funktion nicht. Kenne mich mit PHP leider nicht so gut aus..
Kann mir jemand dabei weiterhelfen?
Freundliche Grüsse
- Anmelden oder Registrieren um Kommentare zu schreiben
theme_links
am 22.07.2008 - 10:03 Uhr
Moin!
Die Funktion
theme_links
gehört in die template.php:<?php
function zen_theme_links($links, $attributes = array('class' => 'links')) {
...
}
?>
hth,
Stefan
Tipp: Beachte die Verhaltensregeln des DrupalCenter.
Die Funktion habe ich in die
am 22.07.2008 - 11:01 Uhr
Die Funktion habe ich in die template.php eingefügt, doch irgendwie will sie nicht funktionieren. Es erscheint auch keine Fehlermeldung beim Aufruf meiner Seite. Ich vermute mal, dass die Funktion gar nicht angesprochen wird (vermutlich wird ja die normale theme_links angesprochen)....
Der Code in der template.php sieht nun so aus:
<?php
function zen_theme_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = '';
//MY CHANGES:
//Search for 'active' in $key and append to $class for LI.
if (strpos($key, '-active') !== FALSE) {
$class = 'active ';
}
// END MY CHANGES
// Automatically add a class to each link and also to each LI
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' ' . $key;
$class .= $key;
}
else {
$link['attributes']['class'] = $key;
$class .= $key;
}
// Add first and last classes to the list of links to help out themers.
$extra_class = '';
if ($i == 1) {
$extra_class .= 'first ';
}
if ($i == $num_links) {
$extra_class .= 'last ';
}
$output .= '<li class="'. $extra_class . $class .'">';
// Is the title HTML?
$html = isset($link['html']) && $link['html'];
// Initialize fragment and query variables.
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
if (isset($link['href'])) {
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
}
else if ($link['title']) {
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (!$html) {
$link['title'] = check_plain($link['title']);
}
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
?>
Name des themes?
am 22.07.2008 - 11:54 Uhr
Ich vermute mal, dass die Funktion gar nicht angesprochen wird (vermutlich wird ja die normale theme_links angesprochen)....
Heisst das theme den "zen"? Falls nicht, musst Du die Funktion noch entsprechend abändern:
<?php
function NameDesThemes_theme_links($links, $attributes = array('class' => 'links')) {
...
}
?>
hth,
Stefan
Tipp: Beachte die Verhaltensregeln des DrupalCenter.
Danke für deinen Tipp, hat
am 22.07.2008 - 15:24 Uhr
Danke für deinen Tipp, hat aber leider nicht funktioniert. Da es sich nur um 4 Menüpunkte handelt die so schnell nicht ändern werden habe ich nun alle Menüeinträge einzeln per CSS formatiert (also 4 einträge für die Klassen menu-1-1-2-active bis menu-1-4-2-actieve)..nicht die schönste Lösung aber sie funktioniert...
@stBorchert: Trotzdem vielen Dank für deine Mühe.
Freundliche Grüsse