WordPress Page Slug Guide, How to get this using functions

Learn how to get the current page slug in WordPress using PHP. This guide covers multiple methods, including $post ->post_name, get_post_field(), get_queried_object(), and more. Perfect for theme and plugin developers!

In WordPress development, retrieving the current page slug is crucial for customizing themes, plugins, and functions. A slug is the URL-friendly identifier for a page or post—for example, in https://example.com/about-us/, the slug is about-us.

To get the slug in PHP, use get_post_field( ‘post_name’, get_post() ) for posts and pages. Alternatively, get_query_var(‘name’) works in certain templates. For categories or taxonomies, use get_queried_object()->slug.

For custom post types, get_post()->post_name retrieves the slug efficiently. If working with WooCommerce, get_post()->post_name also applies to product pages.

These methods help dynamically modify layouts, apply conditional styles, or create custom functionalities based on the current page. Understanding slug retrieval enhances flexibility in WordPress development, ensuring a seamless user experience.

What is Slug in WordPress?

A slug in WordPress is a user-friendly URL identifier for posts, pages, categories, and tags. It helps create clean, SEO-friendly URLs by converting titles into lowercase words separated by hyphens.

For example:

  • Post Title: “10 Best SEO Tools”
  • Slug: 10-best-seo-tools
  • URL: https://example.com/10-best-seo-tools/

By default, WordPress automatically removes special characters and replaces spaces with hyphens to ensure readability. Slugs play a crucial role in search engine optimization (SEO) and content organization.

You can customize slugs manually in the WordPress editor under the Permalink section or modify them programmatically using PHP. Understanding slugs is essential for better URL structuring, improving search rankings, and enhancing user experience. removes special characters from slugs.

Why Retrieve the Current Page Slug?

There are multiple use cases where retrieving the slug is beneficial:

  • Custom styling or scripts: Load specific styles/scripts based on the page slug.
  • Conditional logic: Display different content for specific pages.
  • Dynamic breadcrumbs: Create SEO-friendly navigation breadcrumbs.
  • Custom templates: Assign different templates dynamically.

Similar WP Function How to Check Active WordPress Plugin Using PHP

Methods to Get the Current Page Slug in WordPress

Retrieving the current page slug is essential for dynamic content customization in WordPress. Below are different methods to get the slug using PHP, each suited for different scenarios.

1. Using $post->post_name (For Single Posts or Pages)

If you are within “The Loop” or working with a global $post object, you can access the post_name property to retrieve the slug.

global $post;
if ($post) {
echo $post->post_name; // Outputs the slug
}

This method is effective when dealing with single posts or pages. However, it won’t work for archives or category pages.

2. Using get_post_field()

Another reliable method to fetch the slug is by using get_post_field(), which retrieves a specific field from a post.

$slug = get_post_field('post_name', get_queried_object_id());
echo $slug;
  • get_post_field(‘post_name’, ID): Fetches the post_name field (slug) for a given post ID.
  • get_queried_object_id(): Gets the ID of the current queried object.

This method works for both pages and posts.

3. Using get_queried_object()

WordPress provides get_queried_object() to retrieve details about the current page url, post, or taxonomy.

$queried_object = get_queried_object();
if ($queried_object && isset($queried_object->post_name)) {
echo $queried_object->post_name;
}

This method is flexible and works for various content types.

4. Getting the Slug in Category and Tag Pages

If you’re on a category or tag archive page, you can get the slug using:

$queried_object = get_queried_object();
if ($queried_object && isset($queried_object->slug)) {
echo $queried_object->slug;
}

For example, if you have a category named “News” with a slug news, this will output news.

5. Retrieving the Slug from the URL

If other methods fail, you can extract the slug directly from the URL.

$slug = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
echo $slug;

This method is not the most reliable because it doesn’t consider permalink structures or rewrites.

Using the Slug in WordPress Templates

You can use the retrieved slug for various purposes in theme development.

1. Applying Body Classes

Modify the <body> class based on the page slug:

function add_custom_body_class($classes) {
if (is_singular()) {
global $post;
$classes[] = 'slug-' . $post->post_name;
}
return $classes;
}
add_filter('body_class', 'add_custom_body_class');

This will add a class like slug-about-us to the <body> tag.

2. Enqueueing Different Stylesheets for Specific Pages

Load a unique CSS file for a specific page:

function enqueue_custom_styles() {
if (is_page('contact-us')) {
wp_enqueue_style('contact-style', get_template_directory_uri() . '/css/contact.css');
}
}
add_action('wp_enqueue_scripts', 'enqueue_custom_styles');

This ensures that the contact.css file loads only on the “Contact Us” page.

3. Showing Different Content Based on Slug

Conditionally display content using the slug:

$slug = get_post_field('post_name', get_queried_object_id());

if ($slug === 'about-us') {
echo '<p>Welcome to our About Us page!</p>';
} elseif ($slug === 'services') {
echo '<p>Explore our services.</p>';
}

4. Customizing Breadcrumbs with the Slug

Breadcrumbs enhance navigation and SEO. Here’s a basic way to display breadcrumbs using the slug:

function custom_breadcrumbs() {
if (!is_front_page()) {
echo '<a href="' . home_url() . '">Home</a> » ';
echo '<span>' . get_post_field('post_name', get_queried_object_id()) . '</span>';
}
}

This outputs:
Home » about-us

Summary Table: Best Methods to Retrieve the Page Slug

This table provides a quick comparison of the best methods to get the current page slug in WordPress. Different approaches work depending on whether you’re dealing with single posts, pages, categories, or archives.

MethodWorks forCode Example
$post->post_nameSingle post or page (inside The Loop)global $post; echo $post->post_name;
get_post_field()Posts & pages (outside The Loop)get_post_field(‘post_name’, get_queried_object_id());
get_queried_object()All content types$obj = get_queried_object(); echo $obj->post_name;
$queried_object->slugCategories & tagsecho get_queried_object()->slug;
parse_url($_SERVER[‘REQUEST_URI’])Extracts from URLbasename(parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_PATH));

Conclusion – WordPress View Slug 2025

Retrieving the current page slug in WordPress is essential for customization and dynamic content display. A slug is a user-friendly URL identifier that helps in styling, functionality, and content organization.

To get the current page slug using PHP, several methods can be used depending on the context:

  • $post->post_name retrieves the slug for posts and pages.
  • get_post_field(‘post_name’, get_post()) provides a reliable alternative.
  • get_queried_object()->slug works well for categories, taxonomies, and custom post types.

Using these methods, you can customize WordPress themes and plugins, apply conditional styling, or create targeted content. Whether working on a blog, eCommerce store, or custom project, understanding how to retrieve slugs ensures flexibility and efficiency.

Experiment with these techniques in your projects to enhance WordPress functionality and deliver a more personalized user experience.

Also Check The Complete Guide to Kebab Case: Everything You Need to Know

FAQs related to WP Page Show Slug

1. What is a slug in WordPress?


A slug is the URL-friendly name of a post, page, category, or tag.

2. How do I get the current page slug in WordPress?


Use:

get_post_field('post_name', get_queried_object_id());

3. How do I get the slug of a category or tag page?

get_queried_object()->slug;

4. Can I get the slug without using global $post?


Yes, use get_post_field(‘post_name’, get_queried_object_id());.

5. How do I use the slug in a conditional check?

if (get_post_field('post_name', get_queried_object_id()) === 'about-us') {
echo "This is the About Us page.";
}

6. How can I add the slug as a <body> class?

phpCopyEditadd_filter('body_class', function($classes) {
$classes[] = 'slug-' . get_post_field('post_name', get_queried_object_id());
return $classes;
});

7. Is it possible to get the slug from the URL directly?

Yes, but not recommended. Use:

basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

8. Does this work with custom post types?

Yes, all methods work for custom post types using post_name.

9. What’s the most reliable way to get the slug?

get_post_field(‘post_name’, get_queried_object_id());

10. Can I use the slug to load different styles?

Yes, check the slug and enqueue styles accordingly in functions.php.

Similar Posts