0

I tried this code to hide the primary menu block from certain pages using the following code:

// 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, 13);
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;

Currently, the block is set to visible in the header section of the site; the theme being used is the Bluemarine theme.

Is there anything else that needs to be done?

Print Screen

Print Screen

https://i.sstatic.net/m6y4I.jpg

Menu Print Screem

Menu Print Screem

https://i.sstatic.net/yUBD2.jpg

3
  • Don't use arg(1) to load the node, trust the API and use menu_get_object(). api.drupal.org/api/drupal/includes--menu.inc/function/…. Also, why is this tagged as "drupal-theming", block visibility is not a theming concern, or at least it shouldn't be. Commented Oct 4, 2011 at 7:47
  • can you show how to use menu_get_object() with the above code ?
    – Gopipuli
    Commented Oct 4, 2011 at 9:54
  • "if ($node = menu_get_object()) { ...do something with $node... }" instead of "if (arg(0) == 'node' && is_numeric(arg(1))) { $nid = arg(1); $node = node_load(array('nid' => $nid)); ...do something with $node... }". Commented Oct 4, 2011 at 13:13

1 Answer 1

2

You can do it from the block configuration

  1. go to admin/build/block

  2. search for the block and click "configure"

  3. in the "Page specific visibility settings" click on "Show on only the listed pages"

  4. write the page that you want this block to be visible on E.g "news" this will make that block visible on http://www.examble.com/news

Is that what you mean?

UPDATE:

if you want to control tha appearance programatically do the following

1- Disable the primary links block from "admin/build/block"

2- enable the primary links menu from your theme configuration (now i suppose you see the menu in all the pages and you want to specify the pages that you want this menu to appear in)

3- open page.tpl.php file

4- go to line 24 you will find something like this

<?php if (isset($primary_links)) { ?><?php print theme('links', $primary_links, array('class' => 'links', 'id' => 'navlist')) ?><?php } ?>

we will edit the following condition if (isset($primary_links))

now you should know something if you have url like this http://yoursite.com/media/pictures

then the function arg(0) will return media and arg(1) will return pictures

another example

http://yoursite.com/admin

then the function arg(0) will return the first argument in your url which is admin

now lets back to the condition if (isset($primary_links))

we will edit it ... we wanna say to drupal if the first argument is equal to something then appear the menu else disable the menu

suppose that you want the menu to appeare in the page user

then you will edit the if (isset($primary_links)) to be

if (isset($primary_links) && (arg(0) == 'user') )

suppose that you want this menu to appeare in page user and page admin

if (isset($primary_links) && (arg(0) == 'user' || arg(0) == 'admin') )

one more advanced example... if you want the menu to appeare only in user/1 page and admin page

if (isset($primary_links) && ( (arg(0) == 'user' && arg(1) == '1') || arg(0) == 'admin') )

hope that will help you

28
  • yes this is exactly what am looking for, but when I put show on listed pages node/1, node/13 (separate lines) still the primary menu block is appearing in all pages, I tried the above code also but still no result ? anything I missed ?
    – Gopipuli
    Commented Oct 4, 2011 at 7:25
  • no you are on the right way .. just clear all the code you have inserted before...make sure that each path is on a line E.g node/1 in on line one node/13 is on line 2...and so on ... cleare the cache if there is no effect...if u still have the problem try to change the theme to see if the problem in the theme it self or what...(but i dont think its related to the theme)...but try what i told u above
    – maged adel
    Commented Oct 4, 2011 at 7:49
  • I had manually removed all records from cache tables from mysql, still the primary link block (visibility set in header section in block page) configured to show in list of pages - <front>, node/1, node/13 list in seperate lines(no comma at end) is still showing in all pages of the site.
    – Gopipuli
    Commented Oct 4, 2011 at 9:17
  • can you attach print screen of ur block configuration please
    – maged adel
    Commented Oct 4, 2011 at 10:02
  • I had put the screenshot above ? please let me know whether am correct or not ?
    – Gopipuli
    Commented Oct 4, 2011 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.