Article written

  • on 05.03.2009
  • at 04:17 PM
  • by arkin

How to get the current Wordpress Page Name? 8

Mar5

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 :)

subscribe to comments RSS

There are 8 comments for this post

  1. JOELEANE! says:

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

  2. arkin says:

    No, the page name is the page name :)

  3. Blake says:

    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?

  4. arkin says:

    Learn PHP, Wordpress becomes incredibly easy when you are fluent in the language it is written in :)

  5. Hey,

    Thanks for the tip. Very useful!

  6. sharon says:

    nice trick, but doesn’t seem to work with sub-pages, my current struggle…..

    >: |

  7. dabe says:

    You could do: get_query_var(’name’) to get the name of current post/page.

  8. ugh says:

    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?

Please, feel free to post your own comment

* these are required fields

arkinEx Codex is powered by WordPress and FREEmium Theme.
developed by Dariusz Siedlecki and brought to you by FreebiesDock.com