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

覆写drupal 分类,读取分类id,分类名称,taxonomy term

需求:获取drupal 一个node下面的所有分类id或者分类名称,然后重写terms。默认主题,一下就全部输出了一篇文章的所有分类。如果想自己读取分类,然后分别处理的话,怎么解决呢?
解决方案一:可以在drupal template.php里面来处理,在默认的主题node.tpl.php里面可以找到一下代码
其作用就是输出一个Node下面的所有分类terms。对于初学者来说不知道如何随自己的意思来处理$terms.

<?php if ($taxonomy): ?>
    <div class="terms"> <?php print $terms ?></div>
    <?php endif;?>

解决方法程序如下:先建立一个template.php文件,在你的主题目录下面,和node.tpl.php在同一个目录。
然后把下列代码拷贝进去即可:

<?php
function  yourthemename_preprocess_node(&$variables){//yourthemename改为你自己的主题名称
   
$terms = &$variables['node']->taxonomy;
    if(
is_array($terms)){
       
$t = array();
        foreach (
$terms as $kid => $term) {
          
$t[] =l(t($term -> name),'taxonomy/term/'.$term->tid);
        }

       
$variables['terms']='Tags:'.implode(",",$t);
    }
}
?>

输出效果:

Tags:分类名称

评论

很多余的脑残应用。 直接使用contemplate,设定就

很多余的脑残应用。
直接使用contemplate,设定就可以

呵呵,这里更加简单,直接在主题模块里面写就可以实现。 你去

呵呵,这里更加简单,直接在主题模块里面写就可以实现。

你去下载Content Templates,也可以实现,只不过你多装了一个模块而已。

这主要是为了学习理解drupal函数应用,drupal theme的开发,实现方法自然有很多种。
你喜欢用cck,views,你可以什么不用,直接在template.php写函数输出。

不是很喜欢views 自己动手,可操作性强

不是很喜欢views
自己动手,可操作性强

现在是这个感觉 之前用views感觉太强大了 但用完后,会

现在是这个感觉
之前用views感觉太强大了
但用完后,会发现,不灵活,想改什么东西不方便,定样式什么的
以后打算自己写 :D

是的,自己写方便多了,自己定义theme,想怎么都可以。

是的,自己写方便多了,自己定义theme,想怎么都可以。

能否根据术语表的不同区分输出呢?谢谢。 例如: 分类一:*

能否根据术语表的不同区分输出呢?谢谢。

例如:

分类一:***、***、*** 分类二:***、***、***

谢谢。。

<?php if ($v_node->taxonomy)

<?php
if ($v_node->taxonomy) {
   
// Let's iterate through each term.
   
foreach ($v_node->taxonomy as $term) {
     
// We will build a new array where there will be as many
      // nested arrays as there are vocabularies
      // The key for each nested array is the vocabulary ID.    
     
$vocabulary[$term->vid]['taxonomy_term_'. $term->tid]  = array(
       
'title' => $term->name,
       
'href' => taxonomy_term_path($term),
       
'attributes' => array(
         
'rel' => 'tag',
         
'title' => strip_tags($term->description),
        ),
      );      
    }
   
// Making sure vocabularies appear in the same order.
   
ksort($vocabulary, SORT_NUMERIC);
   
// We will get rid of the old $terms variable.
   
unset($vars['terms']);
   
// And build a new $terms.
   
foreach ($vocabulary as $vid => $terms) {
     
// Getting the name of the vocabulary.
     
$name = taxonomy_vocabulary_load($vid)->name;
     
// Using the theme('links', ...) function to theme terms list.
     
print_r($terms);
     
$terms = theme('links', $terms, array('class' => 'links inline'));
     
// Wrapping the terms list.
     
$vars['terms'] .= '<div class="vocabulary taxonomy_vid_';
     
$vars['terms'] .= $vid;
     
$vars['terms'] .= '">';
     
$vars['terms'] .= $name;
     
$vars['terms'] .= ':&nbsp;';
     
$vars['terms'] .= $terms;
     
$vars['terms'] .= '</div>';
    }
  }  
?>

这个倒是未必 views里面的样式比较强大 可以说 叫做

这个倒是未必 views里面的样式比较强大 可以说 叫做 面向对象 自己写累死的 不过代码 总是解决复杂问题的好东西啊

现在输入的文字是带链接地址的(分类,名),如何修改为不带链

现在输入的文字是带链接地址的(分类,名),如何修改为不带链接啊,去掉a只保留文字

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
  • 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.