Only show a view on a certain content type in Drupal
-
18-09-2019 - |
Question
i have build an "About the Author" views block in Drupal. This is linked at the user_id of the creater of the current node, which works great.
However, i now would like to know how to limit the view to certain content types. I do not want it to show on a story, only on blogs. I tried to do it with Arguments but i haven't had any luck so far.
Can anyone help me out?
Solution
I recommend using pathauto to give each node of the type a common URL prefix (a good idea anyway), so you can use a simple block visibility path restriction. For example, you set your content type path pattern to "article/[title]" and then set your block path to "article/*"
OTHER TIPS
no, you can use view's build-in argument validator.
ex. How do you limit a view to a User’s uid value?” Meaning, how can any given view only be seen by the logged in user who owns that content.
Here’s the Views Argument PHP Validator code.
global $user; return $argument[0] == $user->uid;
Simply create your view, go to the block configuration page and use php for the block visibility rules. To show the block only on certain content types, use:
<?php
$match = FALSE;
$types = array('story' => 1, 'page' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
if (isset($types[$type])) {
$match = TRUE;
}
}
return $match;
?>
This code is taken from drupal.org, Overview-approach to block visibility
Better:
<?php
$match = FALSE;
$types = array('story' => 1, 'page' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
$type = $node->type;
if (isset($types[$type])) {
$match = TRUE;
}
}
return $match;
?>