日期:2015-02-16 阅读:3380
dmandwp系统 - wordpress系统和DM系统区块建站>>
http://drupal.stackexchange.com/questions/6638/get-a-node-id-from-the-url
如果要得到节点id,可以这样:
if (arg(0) == 'node') {
$nid = arg(1);
}
也可以用 if (current_path() == 'blog/16') { 。。。 这样的来判断
如果是对象呢?就这样:
if ($node = menu_get_object()) {
// Use the node object.
}
然后就可以通过$node-type来得到内容类型,从而进一步做一些判断
比如我想给内容类型为产品的在body加个product的class名字,就可以:
function avenev_preprocess_html(&$vars) { //好像只有在preprocess_html才能加class
$node = menu_get_object();
if ($node && $node->nid) {
$vars['theme_hook_suggestions'][] = 'html__' . $node->type;
if($node->nid==3287 or $node->type=='product') $vars['classes_array'][] = 'product';
}
....
//如果是分类,也可以这样:
if (arg(0) == 'taxonomy' && arg(1) == 'term') {
$term = taxonomy_term_load(arg(2));
$voc = array('product_category', 'product_tags');
if (in_array($term->vocabulary_machine_name, $voc)) {
$vars['classes_array'][] = 'product term-page-' . $term->vocabulary_machine_name;
}
}
...
}
但是在drupal8里,menu_get_object不再用了,而是这样的:
http://www.phase2technology.com/blog/goodbye-menu-get-object/
而是这样:
$node = Drupal::request()--->attributes->get('node');
------
另外,在一些preprocess functions里,比如page or node里,
可以这样:
function yourtheme_process_page(&$variables) {
if (isset($variables['node'])) {
// Check the node ID or other properties.
}
---------