• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Conditional tag for subpages of a Page in WordPress for Genesis

Last Updated on August 30, 2019 Favorited: 0 times

This tutorial provides the way to conditionally target a selected parent page and its subpages in WordPress. while working with Genesis Framework.

TYEP 1

WordPress offers is_tree() option to create a function and then output a small snippet based on Page ID.

function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
	
	global $post; 
	        // load details about this page
	if(is_page()&&($post->post_parent==$pid||is_page($pid))) 
        return true;   // we're at the page or at a sub page
	else 
        return false;  // we're elsewhere
};

Usage:

add_action('genesis_before_content', 'custom_tree_child', 15);
function custom_tree_child'() {
	
	if ( is_tree(533) ) { 
    echo "This is a child page";
	}
}

Text -> This is a child page <- will output on subpages of a Parent Page with ID 533. You must change it to match with your Parent Page ID.

TYPE 2

This is a similar approach but works directly.

// if a page is a child of a certain page
add_action('genesis_before_content', 'custom_parent_child_pages', 15);
function custom_parent_child_pages() {
	global $post;
	if ( $post->post_parent == '533' ) { //is_page( 'about' ) || '2' == $post->post_parent )
    echo "This is a child page";
	}
}

Change Number to match with your Parent Page ID.

Reference:

https://codex.wordpress.org/Conditional_Tags
https://developer.wordpress.org/reference/functions/is_page/

Categories: Free Content, Genesis Tutorials, WordPress Tutorials Tags: parent, subpage

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service