Drupal文档教程翻译 - 基于不同的节点类型和id输出区块
本站原创编译,转载请给面子,尊重劳动果实,欢迎交流指正。
作为互联网公司,断网一个上午,我勒个去,不给力啊;导致我一上午没有更新内容,这精神损失又该找谁去说找谁去讲,然则今年还剩下的5天年假因为提出离职而搞到只有1.5天,个中心酸与痛苦又谁人知晓...扯了啊,Druapl文档中文翻译吧。
和昨天的“基于不同的URL输出特定的HTML”有些类似,今天的代码片段可以使系统根据不同的节点类型(node type)和节点id(nid)显示或隐藏指定的区块(block)。
使用方法
- 通过管理后台的“Home » Administer » Site building”进入区块管理页面。
- 添加新区块或点击现有区块的“Configure”链接,进入区块编辑页面。
- 在“Page specific visibility settings”中选择“Show if the following PHP code returns TRUE (PHP-mode, experts only).”
- 将下面将要介绍的代码连同其<?php>标签一起复制粘贴到文本域中。
基于节点id
假设我们的目标节点页面为http://www.example.com/node/1和http://www.example.com/node/2
<?php
// Only show block from types array and nodes array
$match = FALSE;
// Which node types
$types = array('page' => 1);
// Which nodes (by nid)
$nodes = array(1, 2);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
if (isset($types[$type]) && in_array($nid, $nodes)) {
$match = TRUE;
}
}
return $match;
?>
在指定的节点类型中显示区块
<?php
// Only show if $match is true
$match = false;
// Which node types
$types = array('book', 'news', 'anothernodetype' );
// Match current node type with array of types
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
$match |= in_array($type, $types);
}
return $match;
?>
在指定的节点类型中隐藏区块
<?php
// Only show if $match is true
$match = true;
// Which node types to NOT show block
$types = array('book', 'news', 'anothernodetype' );
// Match current node type with array of types
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
if(in_array($type, $types)) {$match=false;}
}
return $match;
?>
在不同的节点类型中添加不同的行为
<?php
// Replace 'nodetype-x' with the node types you need
$nodetypes = array('nodetype-1', 'nodetype-2', 'nodetype-3');
if (in_array($node->type, $nodetypes)) {
// Begin your code for if the node's type was listed in the array
print $something;
// End code for if the node type was listed in the array
}
else {
// Begin your code for if the node type wasn't listed in the array
print $somethingelse;
// End code for if the node type wasn't listed in the array
}
?>
译者:Viiiix7210
如需转载,请注明官方英文文档及本人译文的出处,谢谢。查看英文原文: Show a block depending on node type and node id
| < Prev | Next > |
|---|
