Skip to content

Instantly share code, notes, and snippets.

@tinotriste
Last active January 28, 2021 20:29
Show Gist options
  • Save tinotriste/5388318 to your computer and use it in GitHub Desktop.
Save tinotriste/5388318 to your computer and use it in GitHub Desktop.
Wordpress: Display a page parent's slug
<?php
// Display a page parent's slug
$post_data = get_post($post->post_parent);
$parent_slug = $post_data->post_name;
echo $parent_slug;
?>
@flavioamc
Copy link

THANK YOU! Just what I was trying to do, saved my day 👍

@burgiuk
Copy link

burgiuk commented Jan 31, 2018

You can also use get_post_ancestors() as discussed on the WP Codex.

@Xhynk
Copy link

Xhynk commented Mar 7, 2019

This could be simplified even further making use of get_post_field()

$parent_slug = get_post_field( 'post_name', $post->post_parent );

@old-cory
Copy link

old-cory commented Apr 1, 2020

Thanks @Xhynk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment