继续Drupal分享精神 QQ群:107748121 站长QQ:2275288328 skype: hellodrupal【drupal交流+drupal 建站+theme制作】
登录 注册

重写drupal菜单系统,定制 $primary_links 输出

今天又朋友问到,再次分析一下,
可能在做网站的时候会处理菜单,默认的html代码可能不符合要求。
比如我的网站 现在默认的输出格式是

<ul class="links primary-links"><li class="menu-249 first"><a href="/node/101" title="Drupal 商城开发实践点滴【围巾商品站点】">Drupal 商城案例[新]</a></li>
<li class="menu-144"><a href="/taxonomy/term/4" title="">Drupal 主题</a></li>
<li class="menu-141"><a href="/taxonomy/term/3" title="">Drupal 使用</a></li>
<li class="menu-143"><a href="/taxonomy/term/2" title="">Drupal 开发</a></li>
<li class="menu-142"><a href="/taxonomy/term/1" title="">Drupal 模块</a></li>
<li class="menu-181 last"><a href="/forum" title="drupal 中文论坛">Drupal 论坛</a></li>
</ul>

我想在a 中间价格span,这样的格式:
<a href="/taxonomy/term/2" title=""><span>Drupal 开发</span></a>

那怎么办呢?

解决方法一:

在你使用的主题目录下建立一个template.php文件

然后拷贝以下代码:

<?php
/**
*覆盖primary_links 菜单
*/
function phptemplate_links($links,$attributes=array('class' => 'links')){
$output = '';

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

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

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

     
// Automatically add a class to each link and also to each LI
     
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
       
$link['attributes']['class'] .= ' ' . $key;
      }
      else {
       
$link['attributes']['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 '. drupal_attributes(array('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); //这个是默认的输出样式
       
       
$output .=    "<a href='" . $href . "'><span>" . $link['title'] . "</span></a>";//这个是自定义的输出格式了
        // $output .=l(t("<span>".$link['title']."</span>"),base_path() . $href,array('html' => TRUE));//你想怎么$output 随便你祝贺,变量就是这个几个
     
}
      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;

}
?>

记住,刷新缓冲代码,然后看看html源代码是不是变了
这样的格式了。多了一个span

<ul><li class="first menu-255"><a href=""><span>首页</span></a></li>
</ul>

评论

不过这样就不能实现多语言了.呵呵 欢迎交流veryrose

不过这样就不能实现多语言了.呵呵

欢迎交流veryrose#(at)#gmail.com

谢谢

谢谢

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.