How to Get the Parent Featured Image in Children Subpages in WordPress

WPTurbo » Snippets » How to Get the Parent Featured Image in Children Subpages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you a WordPress developer working on a site with parent and child pages? Have you ever wondered how to display the featured image of the parent page on the corresponding child subpage? In this article, we will explore a solution to this common problem by utilizing the power of WordPress functions and custom coding techniques. By the end, you’ll be equipped with the knowledge to get the parent featured image in children subpages effortlessly. So, let’s dive in and learn how to enhance the visual appeal of your child subpages by displaying the featured image from their parent page.

					function wpturbo_get_parent_featured_image($post_id) {
    // Get the parent page ID
    $parent_id = wp_get_post_parent_id($post_id);

    // Check if the parent page has a featured image
    if (has_post_thumbnail($parent_id)) {
        // Get the parent page's featured image
        $featured_img_url = get_the_post_thumbnail_url($parent_id, 'full');

        // Return the featured image URL
        return $featured_img_url;
    } else {
        // If the parent page does not have a featured image, return false
        return false;
    }
}

// Usage: Replace 123 with the ID of the child page
$child_page_id = 123;
$parent_featured_image = wpturbo_get_parent_featured_image($child_page_id);

// Output the parent featured image URL
echo $parent_featured_image;
				

The code snippet given allows you to retrieve the featured image of a parent page in a child subpage. This can be useful when you want to display the same featured image on a child page as the parent page.

To achieve this, a function named wpturbo_get_parent_featured_image() is defined. It takes the parameter $post_id, which represents the ID of the child subpage for which we want to retrieve the parent page’s featured image.

The first step in the function is to get the parent page ID using the wp_get_post_parent_id() function. This function takes the child page’s ID as input and returns the ID of its parent page.

Next, we use the has_post_thumbnail() function to check if the parent page has a featured image. This function takes the parent page’s ID as input and returns true if it has a featured image, or false if it does not.

If the parent page has a featured image, we proceed to retrieve its URL using the get_the_post_thumbnail_url() function. This function takes two parameters: the parent page’s ID and the size of the thumbnail we want to retrieve. In this case, we use the ‘full’ size.

Finally, we return the featured image URL. If the parent page does not have a featured image, we return false.

To use this function, you need to provide the ID of the child subpage for which you want to retrieve the parent page’s featured image. Replace 123 in the code with the actual ID of the child page.

After calling the function, the featured image URL is stored in the variable $parent_featured_image. You can then output this URL using echo.

Register an account to save your snippets or go Pro to get more features.