I would like to make different divs in the template, that show up at different parts of a site: eg. div#1 shows at pages from Menu-01 + it's sub-menus; div#2 shows up at pages from Menu-02 + its' sub-menus, div#3 at Blog pages, and so on.

How can this be achieved? Coming from the Wordpress world it is something like

if ( is_category ('1') ) {  
//something for category 1
if (is_page ('6') ){
 // something for page 6
// other functions for descendant checks possible with custom made checkers in functions.php 

In Drupal I only found the front page check


But I've found no other types of checkings with this method.

Any help please? Thanks.

  • I should think doing this through standard blocks would work easiest, unless there's a particular reason you want to do it with code...
    – Geoff
    Dec 10 '14 at 19:39

I think Geoff is right, drupal is a bit more abstracted from the template than wordpress, rather than putting logic in the theme (divs, templates) you should use blocks to achieve this.

Create a really simple block module (see block module in examples). This would just be just two simple functions, hook_block_info (defining block) and hook_block_view (returning content of block). If you would prefer you could create a custom block through the admin interface, or present a block using views or any other block module (eg node block).

To determine whether your block shows up on a specific page you could use your own custom logic (probably using menu_active_trail) in your block module, specify the block visibility by path in the block configuration, or use Context module .

  • Thanks for the help, I will check the method. Still quite new to Drupal...
    – Gas
    Dec 12 '14 at 6:59

There is many ways to do this: - you might use the arg() values and act based on that

        if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'YOUR-CONTENT-TYPE') {
               // do something

https://www.drupal.org/project/context might be useful for you

"Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a "section" of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Think of conditions as a set of rules that are checked during page load to see what context is active. Any reactions that are associated with active contexts are then fired."


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.