Community Documentation

Display block only for forums

Last updated May 22, 2006. Created by Cvbge on December 30, 2005.
Edited by Heine. Log in to edit this page.

This code returns TRUE when user views 'forum zone' - i.e. forum, forum/* or a node that is a forum topic. This means, if you use that code, you can display block only on pages that are in forum.
This code can be easily modified to do the opposite - return TRUE only for not-forum pages.

<?php
if (arg(0) == 'forum') {
  return
TRUE;
}
if (
arg(0) == 'node' && ctype_digit(arg(1))) {
 
$node = node_load(arg(1));
  if (
$node->type == 'forum') {
    return
TRUE;
  }   
}
return
FALSE;
?>

Comments

small addition

I used this snippet for the opposite effect, as suggested (hiding some blocks on my forum pages), but I found that they re-appeared while adding a new post or replying to one; So I added a couple lines, like so:

<?php
if (arg(0) == 'forum') {
  return
FALSE;
}
if (
arg(0) == 'node' && ctype_digit(arg(1))) {
 
$node = node_load(arg(1));
  if (
$node->type == 'forum') {
    return
FALSE;
  }  
}
if (
arg(0) == 'comment') {
return
FALSE;
}
if (
arg(0) == 'node' && (arg(1) == 'add' || (arg(1) == 'edit'))) {
return
FALSE;
}
return
TRUE;
?>

and it seems to work for me (although it will also hide the block in question when editing anynodes or comments). As a disclaimer, I am not a programmer, and there is probably a much easier and more elegant way to accomplish the same thing... but it does work!

Small addition

I added a little code to yours to change it to only remove the block when adding/editing on the pages related the forum! :)

<?php
if (arg(0) == 'forum') {
  return
FALSE;
}
if (
arg(0) == 'node' && ctype_digit(arg(1))) {
 
$node = node_load(arg(1));
  if (
$node->type == 'forum') {
    return
FALSE;
  } 
}
if (
arg(0) == 'comment') {
return
FALSE;
}
if (
arg(0) == 'node' && (arg(2) == 'forum' && (arg(1) == 'add' || (arg(1) == 'edit')))) {
return
FALSE;
}
return
TRUE;
?>

Another addition

Improved it to only remove block when replying to forums topics:

<?php
if (arg(0) == 'forum') {
  return
FALSE;
}
if (
arg(0) == 'node' && ctype_digit(arg(1))) {
 
$node = node_load(arg(1));
  if (
$node->type == 'forum') {
    return
FALSE;
  }
}
if (
arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
 
$node = node_load(arg(2));
  if (
$node->type == 'forum') {
    return
FALSE;
  }
}
if (
arg(0) == 'node' && (arg(1) == 'add' || (arg(1) == 'edit'))) {
return
FALSE;
}
return
TRUE;
?>

I'm having trouble with it

I'm having trouble with it and would appreciate any help.

I tried this:

<?php
if (arg(0) == 'forum') {
   return
TRUE;
}

if (
arg(0) == 'node' && ctype_digit(arg(1))) {
  
$node = node_load(arg(1));
   if (
$node->type == 'forum') {
          return
TRUE;
}

return
FALSE;
?>

the second IF statement seems to be ALWAYS returning TRUE, even if I am in the admin page or the front. The only thing I can think of is that I have my drupal installed into a directory on my webhost (IE mysite.com/drupal) I have it that way as I'm still learning and need my main page to be up still. Is that the issue or is it something else?

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.