• I have a situation where I want to be able to display the title of a page based on its slug, but I’m not finding a solution for this anywhere. I’m not especially PHP-savvy, unfortunately, or I might be able to just build the code for that based on logic. Does anyone know how to do this off the top of their head?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Have you set your permalink settings?
    Dashboard > Settings > Permalinks:
    Set to custom structure
    If you just want the page slugs to show, you should enter /%postname%/ into the box (though it is frowned upon for performance reasons…you usually should start off with /%year%/ for example.)

    Hope this is what you are looking for!

    Thread Starter WhitG

    (@whitg)

    Hi Sage,

    Thanks, but I’m afraid that’s not what I’m trying to do. I need to display the page title based on a page’s slug (on the page itself, not in the window title). In other words, I need a block of code that says, “insert the title of page [slug] here.”

    Ah…why don’t you change the slug manually so that it will reflect the page’s name? If you have custom permalinks set up it would say https://example.com/page-slug
    (or you could change the slug to make it:
    https://example.com/slug
    )

    If you give me an example of what you are trying to make it look like, I might be able to help you better.

    Edit: Sorry, just realized what you were saying…The slug generally reflects the page title, does it not? Again, if you could provide an example (or the reason why you want to do this) I (and others) may be able to help more.

    Thread Starter WhitG

    (@whitg)

    I’m afraid we’re not communicating here. The actual name of the slug is irrelevant – I’m going to be calling for the *page title* of a given page, based on that page’s slug. It has nothing to do with what anything looks like.

    Again, the instruction would basically be saying: “Hey WordPress, go get this page’s title: [page-slug-name], and display it right here ->”

    If the page’s title is “Oranges are Great!”, and my manually-created page slug is ‘fruit’, then I want this block of code to say, “Print the ‘fruit’ page title here:” and it will print: “Oranges are Great!”

    Hope that makes sense.

    Here you go:

    $page = get_page_by_path($slug);
    $title = get_the_title($page->ID);

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to get title using page slug?’ is closed to new replies.