webappguides
Create Wordpress Theme From Scratch! Build Your Own

How to get page slug name in WordPress

webapps log

webapps Lab
January 12, 2021
ThemeFunctions

If you have ever used a global variable $post, you may have noticed its properties can retrieve a variety of WordPress data.

The $post property post_name can fetch a post slug.

I have tested it in one of the themes I’m developing.

The variable gives a page title with words separated by a dash.wordpress page slug Hence, this is a simple guide telling how to implement post_name in a loop to get page slug in WordPress.

Let us start now.

The shortcode

The source code for the slug name can be written in any WordPress file depending on how you want to use a given WordPress slug.

The image example I posted used the shortcode in a single.php file.

1. <?php

2. global $post;

3. $slug-name = $post->post_name;

4. echo $slug-name;

5. ?>

Get the slug in the loop

The post slug can be retrieved within the while loop.

It is the same loop that is used in index.php, page.php, and single.php to display the posts of the blog

To get the page slug in the loop, the code should be written in the following structure.

1. <?php if(have_posts()):?>

2. <?php while(have_posts()): the_post();?>

3. <?php
4. global $post;
5. $post_slug = $post->post_name;
6. echo $post_slug;
7. ?>

8. <?php endwhile;?>

9. <?php endif;?>

From this code, the actual loop starts at line-2.

But the loop is initiated if the blog contains at least one post, it is line 1 which specifies the conditional statement.

Therefore, lines 4-6 are going to add posts slug on the given page.

Get post slug by id

The WordPress function to get post slug by id is get_post_field which returns a string.

get_post_field should pass at least two parameters to retrieve the slug value.

Although, the function can receive three arguments.

The first two parameters are the most important for retrieving the post by a post id.

Here is how to implement get_post_field.

<?php echo get_post_field('post_name',2);?>

And the result …

wp page slud by id

Here, the function returns the post slug titled ‘sample-page Hello word!’.

The post id is 2.

Therefore, the parameters that get_post_field takes are a string of post_name and the integer representing the id of the post.

The function should be written outside the loop otherwise the same page slug will repeat multiple times.

How get_post_field work?

The three arguments of get_post_field include

  1. string $field
  2. $post for passing the id
  3. $context

From this line <?php echo get_post_field('post_name',2);?>, $field value is post_name and $post value is 2.

The raw of the function uses the get_post function to create $post object by taking $post id from get_post_field second parameter.

The post object calls post_name to get a page slug.

Therefore, the post_name object is called in this form $post->$field.

This $field value is obtained from the get post field function, remember its value is a first argument of the function.

From the code line I wrote, the $field is set to ‘post_name’.

Then a sanitize_post_field function is called to return a post slug basing on the setting provided.

The sanitize_post_field takes in $field value, post->$field property, the id, and the context specifying the post status to be retrieved.

This function has some similarities with get_the_title.

Get page Id from slug

While I was searching for the function to get page if using post slug I found get_page_by_path can be used for this purpose.

But during the implementation, the function didn’t display the ID of the post.

Although, a lot of WordPress Stackexchange answers show the function works.

I believe it may work for you too, this how I implemented it.

Step #1, open functions.php file

Step #2, create a new function, mine is page_id_by_slug.

function page_id_by_slug($pageSlug){

$pageObject = get_page_by_path(‘pageSlug’);

if($pageObject)

{

echo $pageObject -> ID;

}

}

Step #3, open index.php or single.php or page.php, and call page_id_by_slug

<?php page_id_by_slug(‘books-store’);?>

get_page_by_path in details

The get_page_by_path returns a WP_Post object on success or null on failure.

The WP_Post object contains various methods and properties to retrieve post or page data including page ID.

For filtering the required results to be returned, the get_page_by_path passes at least one parameter.

From the above codes, the function passes one parameter only, which is $pageSlug.

Although, the get_page_by_path can receive three arguments.

These parameters include.

  1.  $page_path
  2. $output default OBJECT
  3. $post_type

The other two parameters (second and the last one) have not been used.

Get post slug using wp_query

WordPress get post by name

Summary