WordPress.org

Ready to get started?Download WordPress

Forums

pagename variable (5 posts)

  1. apiraino
    Member
    Posted 7 years ago #

    I'm working on a custom template. A couple of weeks ago, I was trying to find code to get the page slug of the currently viewed Page. After some searching, I found a webpage that said using the following variable:

    $pagename

    on a WordPress Page would return the slug of the Page currently being viewed. I used this in my template like so:

    ...
    if ($pagename=="illustration")
    ...

    It seems to work perfectly, doing exatly what I want. However, I'm trying to find *where* I got that info from, because now, two weeks later, I can't find the page that gave me that info, and looking over the code, I can't figure out *why* this works, since "$pagename" doesn't look anything like a normal wordpress template tag or function.

    Just for additional info, I'm not passing any query string variables in my urls - this works on pages with urls like "localhost:4000/illustration/".

    Can anyone tell me if this is actually a legitimate way to get the slug name of a page?

  2. openpundit
    Member
    Posted 7 years ago #

    I don't know if that is the "proper" way to get the name of a page. You said it works though right? So if it works, then it works. It is doing what you want it do.

    What exactly are you trying to do now with it?

    Thanks for that info too, you just gave me an idea.

  3. apiraino
    Member
    Posted 7 years ago #

    "So if it works, then it works."

    Yeah, part of me thinks that way too, but then another part thinks that if it's not "proper" it could abruptly stop working in a subsequent WP update without niceties like deprecation or any kind of warning. I'm also curious why it seems like such an easy, straightforward way to do this, yet is not mentioned *anywhere* (except for on the one mystery page that I can't find anymore).

    What I'm trying to do is use the current page name in some conditional logic - i.e., if you're looking at Page1, show X, and if you're looking at Page2, show Y.

    I tried an alternate way of doing it that I've seen elsewhere, using

    $post->post_name;

    to get the page name. However, some of my Pages display post content on them, and that line of code then returns the slug for the post rather than the Page, which is not what I'm looking for.

  4. openpundit
    Member
    Posted 7 years ago #

    Oh ok, something like this:

    <?php if ( is_page('2') )
    
     echo 'This is just one of many fabulous entries'
    
    { ?>

    I may have it wrong...

  5. apiraino
    Member
    Posted 7 years ago #

    Sort of, but unfortunately it's more complicated.

    I am using is_page('name') for some of what I'm doing, but for this particular part it would be more like:

    if (is_page('2') || is_page('3') || is_page('4')) {
    do some stuff
    if (is_page('3')) {
    do some additional stuff
    }
    if (is_page('4')) {
    do yet some other stuff
    }
    }

    just as an approximation. Some of the "stuff" I have to do could be simplified if I had access to the current page name, rather than having to do multiple checks for it.

Topic Closed

This topic has been closed to new replies.

About this Topic