Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Using Functions in Place of Globals: get_post_field
<?php
/**
* Retrieves the slug for the current post via the current post's
* post_name property.
*
* @return string the slug of the post.
*/
function getPostSlug(): string
{
global $post;
return $post->post_name;
}
<?php
/**
* Generates a boolean value based on the presence of a post slug.
*
* @return bool True if there is a slug; otherwise, false.
*/
function hasPostSlug(): bool
{
return ('' === getPostSlug());
}
/**
* Retrieves the slug for the current post via the get_post_field
* API function.
*
* @return string the slug of the post.
*/
function getPostSlug(): string
{
return get_post_field('post_name', get_the_ID());
}
<?php
if (hasPostSlug()) {
echo getPostSlug();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.