Hi there,

can somebody please tell me, how to show a block on pages being of a specific content-type?
I'm guessing it has to be something in "Show if the following PHP code returns TRUE (PHP-mode, experts only)."

Many thanks :)

Comments

dcomfort’s picture

There is a new module which might provide a solution: MultiBlock

"Drupal's block module is limited by the fact that a block can only have one instance. Each block has a 1:1 relationship with its region, weight, visibility (and other) settings. This means that it is impossible to have blocks in multiple regions or to have blocks that have different settings on different pages. MultiBlock module solves this problem by allowing you to create multiple Block Instances of already existing blocks. Please read the README for more information."

http://drupal.org/project/multiblock

David Comfort

nevets’s picture

You are on the right track, you want to place something like the following there

<?php
// Change the type to the one you want to match on
$desired_type = 'story';
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
   return $node->type == $desired_type;
}

return FALSE;
?>
quotientix’s picture

Thanks, that's working perfectly fine.

I have another question:

can you please tell me, how to change the php-code so that the block is showing on:

Type Musical
Type Play
View named "Musicals"
View named "Plays"

Many thanks, my php-knowledge is as bad as it gets!

nevets’s picture

This should help, note the comment on types

<?php
// The content types you want to match on
// These should come from the second column (Type) of admin/content/types
$desired_types = array('Musical', 'Play');
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
   return in_array($node->type, $desired_types);
}

// Or is it a path we care about
$path = $_GET['q'];
$desired_paths = array('Musicals', 'Plays');


return in_array($path, $desired_paths);
?>
quotientix’s picture

Many thanks, nevets!

It's working perfectly fine! It would have taken weeks for me to figure it out myself :-D

bennyjs’s picture

This script works perfectly for me. Thanks. However, I'd like to add a wildcard to one of my paths so that the block will display no matter what argument has been added on to the end.

For example, I have a taxonomy menu that creates paths like this: project/10 and project/11.
I'd like my block to be visible for any path that includes project/*. I also want it to display on my /gallery node.

So, in the second part of the script that checks the path, I've tried the following:

// Or is it a path we care about
$path = $_GET['q'];
$desired_paths = array('gallery', 'project/%');

Now the block is visible on the gallery page, but not the project/* page. I've also tried replacing the % with a *, but neither worked. Any suggestions?

nevets’s picture

Is project/10 are real path or an alias?

bennyjs’s picture

Well, I'm not sure I know the answer to that. I have the Taxonomy Menu module installed, which lets me create a custom path for my taxonomy terms. So, rather than taxonomy/term/tid, the path to each term id is project/tid.

That sounds like an alias to me, but I'm not completely sure.

atonaldenim’s picture

Hi, thanks for the code, this is exactly what I want to accomplish...

But what I really want is to show the block always, EXCEPT on a few specific content types and/or URLs.

How would you reverse this code so it returns true on all content types except those you specify?

Thanks!

nevets’s picture

Reversing my earlier example so it shows on all but some selected pages negate the return values like this

<?php
// The content types you want to match on
// These should come from the second column (Type) of admin/content/types
$desired_types = array('Musical', 'Play');
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
   return !in_array($node->type, $desired_types);
}

// Or is it a path we care about
$path = $_GET['q'];
$desired_paths = array('Musicals', 'Plays');


return !in_array($path, $desired_paths);
?>
shunting’s picture

... it's cheaper to use a node that's already built via:

$node = menu_get_object(arg(1));

than to use:

$node = node_load(arg(1));

This thread solved my issue, though!

nevets’s picture

Note in this case menu_get_object() also gets the node from node_load() and both cases the node is cached.

spinsheet’s picture

This was all incredibly helpful. I have this working on a specific content type but I would also like it to appear on any page with a specific path, such mysite.org/climate-change. So far I am using the following code:

<?php
// Change the type to the one you want to match on
$desired_type = 'ccvi_assessments';
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
   return $node->type == $desired_type;
}

return FALSE;
?>

How would I also add the URL alias climate-change?

Thanks again for the help with this!