WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page ID from Page Name (4 posts)

  1. joshrodgers
    Member
    Posted 2 years ago #

    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

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

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

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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.

  4. joshrodgers
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic