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"?
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"?
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.
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!
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.