views:

53

answers:

4

I want to use the if statement to select specific pages that have <body class="section-category"> and output custom content for only those pages.

My PHP is not very good and I hope this is a very simple task to do. Can anyone give me a tip?

A: 

Edit after the question update:

This is what I mean by using the $body_class variable:

<?php if (stripos($body_class, 'column sidebar')): ?>
  This is the page
<?php else: ?>
  This is not the page
<?php endif; ?>

If you're not inside page.tpl.php, you can use drupal_set_title("The text").

Otherwise, inside page.tpl.php, the variable $head_title contains this information. Here is a full list of the variables you have access to in page.tpl.php (some themes add even more variables).

Example:

echo "<title>$head_title</title>";
wildpeaks
I'm sorry my question was incomplete because I didn't add <code> tags. I have edited my question to be more clear.
Chris
If all you know is that that specific page is this class name, you could look in to the "$body_class" variable which contains the value of the "class" field.
wildpeaks
A: 

I would look into a module like Context or Panels to do this- it will serve you better in the long run than coding it in.

Kevin
+1  A: 

I would not check for the existence of a certain body class. Those classes are only the results of other if-else logic and they can easily be overridden or altered, breaking your page template. It's better to check the values those classes are based upon. If I were you, I would try to figure out how this body class was generated and re-use that code.

For example, if your theme's template.php does something like this:

$body_classes[] = 'section-' . form_clean_id(arg(0));

Then I would put this in my template.php:

<?php if (form_clean_id(arg(0)) == 'category'): ?>
  // Do fancy stuff!
<?php endif; ?>
marcvangend
Thanks for the tip. The page is created by the panels module. So would you know how to get the code from that? Here is the exact body tag: <body class="not-front not-logged-in no-sidebars page-category-business section-category page-panels">
Chris
marcvangend
A: 

It sounds like you want a flag available for certain pages; one that you can read out and act upon in your theme.

In template.php:

function YOURTHEME_preprocess_page(&$vars) { //Many themes already have this function implemented
  if (stripos($vars['foo-bar'], 'section')) {
    $vars['is_section'] = TRUE;
  }
  else {
    $vars['is_section'] = FALSE;
  }
}

Then in your page.tpl.php

<?php if ($is_section): ?>
    <p>I am in a section</p>
<?php endif; ?>

That way you keep logic where it belongs: in the preprocessors. And you keep logic out of the template, where it most certainly does not belong! The if (stripos($vars['foo-bar'], 'section')) { can most probably be made a lot smarter, if you do a var_dump($vars) there, all available variables will dump on your screen (browser). I am certain you will find a variable that you can check against, and that is less 'fuzzy' then a body class. After all: that class is meant for one thing only: to serve as class in the body-tag. And not for checking if you are in some section. You have the full Drupal available there too, so you can even use any function that (e,g, the section) modules provide.

berkes
Hi Berkes. I pasted that code into my template.php file and changed YOURTHEME to my theme name. I also exchanged 'foo-bar' for 'category' which is the section that I want flagged. After clearing cache and refreshing, I get a blank page. Do you know why this is? I also tried using phptemplate instead of YOURTHEME.
Chris
By the way I'm using the STARTERKIT theme from Zen.
Chris