php obtener slug wordpress
Step 1: Open the WordPress theme's functions.php file.
Step 2: Use the get_post
function to retrieve the post object based on the post ID.
Step 3: Use the get_post_field
function to retrieve the post name (slug) from the post object.
Step 4: Assign the post name (slug) to a variable for further use.
// Step 1
function get_post_slug($post_id) {
// Step 2
$post_object = get_post($post_id);
// Step 3
$post_slug = get_post_field('post_name', $post_object);
// Step 4
return $post_slug;
}
Step 5: Call the function with the desired post ID to obtain the post slug.
// Example usage
$post_id = 123; // Replace 123 with the actual post ID
$post_slug = get_post_slug($post_id);
Note: Replace "123" with the actual post ID you want to retrieve the slug for.