Is there a function similar to <?php if ($is_front): ?>, to check if the current page is the contact page (for example) ?

Comments

paean99’s picture

You could use $title to compare to the title of your contact page (it can be different depending on the site).

Stijn Geens’s picture

Hi paean99,

I tried to use <?php if ($title != ‘Contacteer ons’): ?>, but then I get the error "syntax error, unexpected T_STRING".

paean99’s picture

<?php if ($title != ‘Contacteer ons’): ?> has the wrong apostrophes and it should be !== not !=
You should also be certain that 'Contacteer ons' is the correct title. You should already have a <?php print $title; ?> in your page.tpl.php to help you discover it.
This solution can be accompanied with an <?php if ($title): ?>. An isset tactic might be necessary here since the title is not an absolute in every pages. Although in the case of the drupal contact form it isn't normally a concern.

The current_path() is a better and more general solution. if(current_path() == 'contact') is correct if your path for the contact form is mysite.com/contact. It could be different in your case...

Any of these solutions can be broken if you later change the title or the url.

Stijn Geens’s picture

Thank you, this is very helpful. I used the current_path() function and it works like a charm.

Jaypan’s picture

There isn't a function, but you can use:
if(current_path() == 'contact')

Stijn Geens’s picture

Hi Jaypan,

When I use your code I get the error "Use of undefined constant ‘contact’ - assumed '‘contact’' in include()".
I'm using this code in page.tpl.php from the Venture theme.

What am I doing wrong?

Jaypan’s picture

What am I doing wrong?

You're not showing us your code.

Stijn Geens’s picture

I'm sorry. Here is the code that i've used:

<?php if (current_path() == ‘contact’): ?>
...
<?php endif; ?>
paean99’s picture

Wrong apostrophes in this case also. The character code is different.
Use this instead

<?php if (current_path() == 'contact'): ?>
...
<?php endif; ?>

It seems the same but it has the correct apostrophes. The colours should give you a hint.

Stijn Geens’s picture

It works like a charm now. Thanks!