Home › Forums › General Issues › Show ACF if is child of spec page Parent XYZ › Reply To: Show ACF if is child of spec page Parent XYZ
If I understand your question correctly, you want to display an Advanced Custom Fields (ACF) field if the current page is a child page of a specific parent page, called “Parent XYZ.”
To achieve this, you can use the get_post_ancestors() function to check if the current page has “Parent XYZ” as its parent page. If it does, then you can use the get_field() function to display the value of the ACF field.
Here is an example code snippet that you can use:
php
Copy code
<?php
// Get the current post ID
$current_id = get_the_ID();
// Get the list of ancestor IDs for the current post
$ancestors = get_post_ancestors($current_id);
// Check if “Parent XYZ” is in the list of ancestor IDs
if (in_array($parent_xyz_id, $ancestors)) {
// Display the value of the ACF field
$field_value = get_field(‘your_acf_field_name’);
echo $field_value;
}
?>
In this code, replace your_acf_field_name with the name of your ACF field, and $parent_xyz_id with the ID of the “Parent XYZ” page.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.