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?
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?
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>";
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.
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; ?>
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.