Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Garconis/2ee966b8ecc2a3bbd3ab50f65ba6daa3 to your computer and use it in GitHub Desktop.
Save Garconis/2ee966b8ecc2a3bbd3ab50f65ba6daa3 to your computer and use it in GitHub Desktop.
WordPress | Check if parent page has child page of certain slug
<?php
function fs_sc_location_seo_iconbox( $atts ){
// begin output buffering
ob_start();
global $post; // if outside the loop
$slug_to_check = 'seo';
// get any child pages of this parent page
$child_pages = get_pages( array( 'child_of' => $post->ID ) );
// go through each child page we found
foreach( $child_pages as $child_page ) {
// get the slug of this child page we found
$child_slug = $child_page->post_name;
// get the link of this child page we found
$child_link = get_page_link( $child_page->ID );
// if the child page we found has the slug we want
if($child_slug === $slug_to_check) {
// output the child page link
echo '<a href="' . $child_link . '">Our SEO child page</a>';
}
}
// end output buffering, grab the buffer contents, and empty the buffer
return ob_get_clean();
}
add_shortcode( 'fs_location_seo_iconbox', 'fs_sc_location_seo_iconbox' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment