Created
April 22, 2021 19:39
-
-
Save Garconis/2ee966b8ecc2a3bbd3ab50f65ba6daa3 to your computer and use it in GitHub Desktop.
WordPress | Check if parent page has child page of certain slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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