How to get the current WordPress Page Name?
A friend had the question tonight “How do i pull the pagename of the current page in wordpress?.”
Turns out he wanted to display different padding around his paragraphs on different pages.
Here is the solution I came up with:
The code, which can be placed at the top of header.php in your theme.
<?php list($page_name,) = explode('/', trim(array_shift(explode('?', $_SERVER['REQUEST_URI'])), '/')); $page_name = (!empty($page_name)?$page_name:'homepage'); ?> ?>
Then to echo the page_name in a div or something…
<div id="Site" class="page_<?=$page_name;?>">
Good luck wordpress fanatics
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
Comments
Thanks, I’ve been looking for this. Where do you go to figure this stuff out? Or do you just know enough to fiddle with it until it works?
What’s up with the redirection bs going on on this page? It loads and then unless I stop it redirects to some obtrusive page trying to sell something. Have you been hacked or is that intentional?
If you want to get the slug name try this
Put this function in your function.php page
ID, ARRAY_A);
$slug = $post_data['post_name'];
return $slug; }
?>
Put this script into your page (header is where I have mine)
Now you can use it in the body tag. *this is useful when adding CSS to specific paged. eg: custom banners, different colour, different behaviour for rollovers etc.
<body class="”>
…all the page stuff in between…
OR part of an if statement
if ($slugName == “home”) {
do something eg: load a big home page picture
}
It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed:-~
can you not use “wp_title” to get the pagename?
eg
so on the page echo:
<div id="Site" class="page_”>
^^ seems to have stripped out my code
so above use: wp_title(”,true,”);
to get the pagename in the class

Is page name the same as the page slug in this instance?