Sometimes you just need to get the slug of the current post or page.

One application of this technique I’ve used in my themes is to check if the current post slug has a matching Revolution Slider instance with the same slug. If a match is found, the Revolution Slider instance is added to the page header instead of the default template part.

This simple function returns the trailing name component of the URL. For a post or page, this will match the slug exactly. For a taxonomy archive, this will match the slug used for the taxonomy item.

The Code

Add the following code to your theme’s functions.php file.

// Returns the slug for the current post or page
function get_the_slug(){
	return basename(get_permalink());

Photo by Abbey Lee on Unsplash