Drupal - How to display a block on only the listed pages using wildcard and excluding subpages?

Method 1. PHP filter

I think your question is partially answered here:

How to add page restrictions for blocks in drupal?

Only you might use this code:

<?php
$arg = explode('/', drupal_get_path_alias());
if ($arg[0] == 'about-us' && $arg[1] && $arg[2] == NULL) {
  return TRUE;
}
else {
  return FALSE;
}
?>

Method 2: Context module

Alternatively, you can try the Context module, which offers more robust visibility options. For instance, in addition to including paths you can prefix a path with a tilde "~" to exclude it.

I think this should do it:

about-us/*
~about-us/*/*

Edit: Added second method.

Edit: Using $_GET['q'] in method 1 instead of arg() (Thx, @leymannx!)


Drupal 8 - Simple module to achieve this: https://www.drupal.org/project/block_exclude_pages

Simply add the path you want to exclude with a '!' Prefixed.

Example:

!/path/this

Tags:

Uri

Blocks