WordPress Get Page Slug. The name “slug” comes from the web publication and usually refers to a part of a URL that identifies a page or resource. The name is based on the use of the word slug in the media to provide an abbreviated name for an internal article.
In Award Force, a snail is a short and unique reference to a resource such as an entry, a category, a field, you have the idea!
Award Force snails are always exactly 8 characters long and a random mix of uppercase and lowercase letters. There are many reasons to use random code to refer to certain information, but most of the time it is to have a short and absolutely unique identifier or a pseudonym.
WordPress Get Page Slug
There are several ways to get the snail from the current page or post on WordPress. The easiest way to get the slug of a post or page is to access the post_name property of the global post object. The first method shown below accesses the post_name property within the $post variable, while the second option below uses the get_post_field method.
Put the slug in the loop
WordPress pages are a type of publication, so the $post variable provides you with information about the page. The global post object also contains data associated with the current page or publication that is accessed. This global variable is available in the WordPress loop.
The WordPress loop is “PHP code used by WordPress to display posts” at https://codex.wordpress.org/The_Loop.
To get the page visit in the loop, the code must 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;?>
use the get_post_field method
You can also use the get_post_field function to get the current page or slug post. If you’re inside the loop, it looks like the following code:
If you’re outside the post loop, you need a second argument for the get_post_field function. You can use a value of $post_id for this second argument. See the following code as an example:
There are several ways to get the snail from the current page or post on WordPress. The easiest way to get the slug of a post or page is to access the post_name property of the global post object. The first method shown below accesses the post_name property within the $post variable, while the second option below uses the get_post_field method.
See also:
WordPress Is_page Function
How To Fix: The Page Cannot Be Displayed Because an Internal Server Error Has Occurred?