Support » Fixing WordPress » Page ID from Page Name

  • Resolved joshrodgers

    (@joshrodgers)


    Hello All!!

    I am working with the following code: http://pastebin.com/n4MbfNwY, which is in my functions.php

    Basically, it gives me the ability to return the page id from a page name.

    I have the following code in my template: http://pastebin.com/BgM0GqzH.

    This code works, for the most part. It seems the code doesn’t work when I have a page that’s more than one word. In other words…the pages named “Flower”, “Pot”, or “Plant” will all work correctly…the pages named “The Flower”, “Your Pot”, or “My Plant” will not work at all.

    Any ideas?

    Thanks,
    Josh

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)

    Forum Moderator

    If you are inside the Loop, why not just use $post->ID to grab the page id?

    there is a difference between post_name which is the way the post slug is saved in the database, and post_title which is the one with spaces and capitalisation etc, also in the database http://codex.wordpress.org/Database_Description#Table:_wp_posts

    try to amend the used code of your first pastebin.

    Woo hoo!! Thanks for your help!

    esmi: I had looked at that, but I am using this function outside my loop 🙁

    alchymyth: I changed post_name to post_title in my functions page and now everything works great!! In addition, I looked at the database and I now know where to find the page slug…so I could do it either way 🙂 but, post_title is definitely the easiest, most efficient, route.

    Thanks to the both of you!!! Everything works like a champ!

    Josh

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page ID from Page Name’ is closed to new replies.