-3

I am using the code below to get y name of my wordpress page.

$pagename = $post->post_name;

The problem is that I'm getting a slug.

I get "wedding-cakes" instead of "Wedding Cakes"

How do I get the name "Wedding Cakes"?

John Carson
  • 35
  • 1
  • 6

3 Answers3

3

As per the Wordpress docs relating to the $post object, you will want to do this instead:

$pageName = $post->post_title;

Which will return the post title not the slug. The name actually refers to the slug. From the wordpress docs:

post_title (string) The post's title.

post_name (string) The post's slug.

Community
  • 1
  • 1
Henders
  • 1,195
  • 1
  • 21
  • 27
0

It's SIMPLE! Using the function bellow we get the name of the post and not the slug:

$thePageTitle = get_the_title();

You can add the code below to convert it to a slug:

$thePageSlug = sanitize_title($thePageTitle);

Problem Solved!

John Carson
  • 35
  • 1
  • 6
0

I believe that the property for the title of the post is 'post_title' so you want to use $post->post_title when the Wordpress $post object is available to you. If it is not then you can use global $post first before you access the property.

weaveoftheride
  • 4,092
  • 8
  • 35
  • 53