1

I have several blocks in my homepage : 2 statics and 1 with a pager.

I want to hide a static block when paging my homepage (after page "0").

I saw something similar in How can I show a block in a view page, excluding it's paginated pages but I do not know exactly what to put in the PHP code regarding the homepage : '' or '/' (instead of 'news' in this example) ?

<?php
$url = request_uri();
$pos = strpos($url, "page");
if ($pos === false && arg(0) =='news') {
return TRUE;
}
?>

I tried also this code that I saw on this answser : How to add page restrictions for blocks in drupal? but it does not work for me.

<?php
if (arg(0)=='/' && is_numeric(arg(1))) {
return TRUE;
} else {
return FALSE;
}
?>

My website is : http://femme.bonsplansdemarc.fr/

0
1

Your suggestions both involve using the PHP setting for block visibility, which isn't available by default and isn't recommended for security reasons. If you already have PHP filter enabled and want to use it, you can probably use the code:

<?php
  if (drupal_is_front_page() && !isset($_GET['page'])) {
      return TRUE;
  }
  return FALSE;
?>

The option I would recommend (which avoids using PHP filter) would be to install the context module (with this patch https://drupal.org/node/904416) and position the blocks using that. You can set it to appear in all circumstances except when on the home page with query string '*shs_term_node_tid_depth=*'.

If you don't know how to use context module, there are lots of tutorials and it's fairly simple.

2
  • Thanks for the tip. I have been trying with your PHP code, it works fine for de Home Page, but my block now appears also in all pages of the web site.
    – Emmanuel
    Mar 14 '14 at 10:58
  • I have edited my comment to show the block only on the homepage when the pager is not set.
    – leexonline
    Mar 16 '14 at 22:28
1

There is another alternative available ... which doesn't require the PHP filter to be enabled (which you should try to avoid whenever possible) ... Just use the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the block is only shown on your homepage but not after page "0").

For an illustration of how to use this module, refer to my answer to "How to show block on pages with URLs containing a question mark ( 'searchquery' )?". If you'd use the sample rule in that answer, you probably want to use a "NOT" (= negate) in the Rules Condition there, and of course you'd have to adapt the "viewname?" value in it to match your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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