How can i get the name of a page's parent?
I can only get it's id.
$page = get_queried_object();
$pageparent = $page->post_parent;
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityHow can i get the name of a page's parent?
I can only get it's id.
$page = get_queried_object();
$pageparent = $page->post_parent;
Once you've got the id of a post or page, use get_the_title($id)
to get the page title (which is what I assume you mean by name)
Note that get_queried_object
will only work on pages with a single post (or page) on them. For example, if you were trying to find a parent of a post on a category archive page, get_queried_object
would return the category, not the current post.
So, using the following would work in more circumstances:
global $post;
$parent_title = get_the_title( $post->post_parent );
global $wpdb;
$page_id = YOUR_PAGE_ID; //your page id here.
$parent_page_id = $wpdb->get_var( $wpdb->prepare(
"
SELECT post_parent
FROM $wpdb->posts
WHERE ID=$page_id;
"
) );
$parent_page_title = get_the_title($parent_page_id);
echo $parent_page_title; //this will print parent page title.