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

  1. joshrodgers
    Posted 4 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?


  2. esmi
    Forum Moderator
    Posted 4 years ago #

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

  3. Michael
    Forum Moderator
    Posted 4 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
    Posted 4 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!


Topic Closed

This topic has been closed to new replies.

About this Topic