Hi,
I’m not great with Wordpress, new enough to it (and PHP).
Sites I have worked on before would have page files like, index.php or about-us/index.php - so in each of these files at the top would be something like this:
$currentLevel1Page = "Home";
Anyone know how I can add something like this to separate pages in Wordpress? I’d like to call a different element, like a paragraph or menu depending on the page.
Cheers.
Al.
I don’t know if this is the best way, but I add
if (is_page()) {
$page_slug = 'page-'.$post->post_name;
}
at the top of the page (in header.php) and then add the class to the body tag with
<body <?php body_class($page_slug); ?>>
That way if I have a page that has the title “About Us”, the body tag will have a class of “page-about-us” among other default classes that WordPress uses. So if I need to target just that page, I can refer to its class name.
Thanks for that WebMachine.
I got a comment in another forum which said to use $_SERVER[‘request_uri’].
What I have at the moment is:
<?php
if($_SERVER['REQUEST_URI'] == '/')
{
echo 'content';
}
?>
And that seems to work. What my problem now is, that I need to get an elseif statement working to get the above code to output different content for whatever page it is on.
I have tried this, but I’m not even sure if it’s right (it doesn’t seem to work though):
<?php
if($_SERVER['REQUEST_URI'] == '/')
{
echo 'content';
}
elseif($_SERVER['REQUEST_URI'] == '/about/')
{
echo 'about content';
}
?>
I have it sorted now using this:
$page = $_SERVER['REQUEST_URI'];
if ($page == '/') {
$content = 'page /';
} else if ($page == '/home') {
$content = 'page /home';
} else if ($page == '/about') {
$content = 'page /about';
} else if ($page == '/contact') {
$content = 'page /contact';
} else {
$content = 'default if page is not above';
}