• Hello,

    I am trying to grab the page name of a page through PHP.
    I have found this code online which is working:

    global $post;
    $pagename = $post->post_name;

    However, my page is example.com/slug/pagename/
    With the code above $post = slug instead of $post = pagename.

    How can I fix that? I tried to use $pagename = $post->"user"->post_name but it didn’t work.

    Thank you in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • post_name retrieves the slug. post_title is what you are looking for.

    https://codex.ww.wp.xz.cn/Function_Reference/$post

    Thread Starter Eibe

    (@eibe96)

    Hey CPC Mike,

    thank you for the reply.

    I have now changed it to:

    global $post;
    $pagename = $post->post_title;

    However, I still get the slug instead of the title.
    To be more specific the “slug” is a page of the plugin “Ultimate Member”, where the user pages are all under /user/USERNAME and the shortcode of the plugin is placed in the “user” page.

    Could that be the problem?

    Note: the title of the page is USERNAME (html source code shows: <title>USERNAME</title>)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘PHP: grab page name’ is closed to new replies.