• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Conditional Tag checkig if the parent of a page is a specific page doesn’t work

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Conditional Tag checkig if the parent of a page is a specific page doesn’t work

This topic is: resolved

Tagged: conditional tag, parent of a page

  • This topic has 2 replies, 2 voices, and was last updated 6 years ago by misionis.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • February 15, 2017 at 8:07 am #201203
    misionis
    Participant

    Hello everybody, my name is Michael and I build websites on WordPress. For not a long time I work with Genesis framework.

    I'm trying to set the conditional tag, that will allow me te set custom, specific sidebar to a specific page and to all of it's child pages without just listing them all (maybe for grandchilds also). I could just list them on my website, but when I build one to my client, it's problematic.

    So, I've putted this to my functions.php:

    genesis_register_sidebar( array(
    	'id'            => 'sidebar-realizacje',
    	'name'          => __( 'sidebar realizacje', 'Genesis Sample' ),
    	'description'   => __( 'tutaj wstawiamy sidebar realizacje', 'Genesis Sample' ),
    ) );
    
    genesis_register_sidebar( array(
    	'id'            => 'sidebar-wiedza',
    	'name'          => __( 'sidebar wiedza', 'Genesis Sample' ),
    	'description'   => __( 'tutaj wstawiamy sidebar wiedza', 'Genesis Sample' ),
    ) );
    
    add_action('get_header','ms_change_sidebar');
    function ms_change_sidebar() {
        
        if ( get_post_type() == 'realizacje' ) { 
            remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
            remove_action( 'genesis_sidebar', 'ss_do_sidebar' );
            add_action( 'genesis_sidebar', 'dodaj_sidebar_realizacje' ); 
        }
    	elseif ( is_page( '18' ) || '18' == $post->post_parent ) { 
            remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); 
            remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); 
            add_action( 'genesis_sidebar', 'dodaj_sidebar_wiedza' ); 
        }
    }
    
    function dodaj_sidebar_realizacje() {
    		genesis_widget_area( 'sidebar-realizacje', array(
    			'before' => '<div class="sb-realizacje widget-area">',
    			'after' => '</div>',
    	) );
    }
    function dodaj_sidebar_wiedza() {
    		genesis_widget_area( 'sidebar-wiedza', array(
    			'before' => '<div class="sb-wiedza widget-area">',
    			'after' => '</div>',
    	) );
    }

    In result, I have propor sidebar in custom post types 'realizacje', and page id'18' as well, but child pages of '18' still have primary sidebar.

    The code snippet is taken from https://developer.wordpress.org/themes/basics/conditional-tags/

    <?php if ( is_page( 'about' ) || '2' == $post->post_parent ) {
        // the page is "About", or the parent of the page is "About"
        $bannerimg = 'about.jpg';
    } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) {
        $bannerimg = 'teaching.jpg';
    } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) {
        $bannerimg = 'admissions.jpg';
    } else {
        $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
    }
    ?>

    I've set php7.0 on my hosting - maybe this is a problem.

    My website:
    http://miastostron.pl/
    '18' - http://miastostron.pl/baza-wiedzy/
    '18' child example - http://miastostron.pl/baza-wiedzy/co-to-jest-anchor-text-linku/

    Thank You very much! 🙂

    http://miastostron.pl/
    February 15, 2017 at 2:47 pm #201261
    Victor Font
    Moderator

    The only thing that I can see is that you are not referencing the global $post variable in your function. You should have global $post; at the top of the function. Also, it could be your timing. The $post variable is in the loop. You are rendering the sidebars in the header, before the loop is called. You might want to look at get_page_children(). https://codex.wordpress.org/Function_Reference/get_page_children


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    March 15, 2017 at 5:22 am #203141
    misionis
    Participant

    Thank You very much for Your answear. The problem is I'm not a php developer and my skills in it are at the beginner level.

    But I've found the solution that works fine. Maybe someone will have similar issue, so I pase it below. Regards.

    function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
        global $post;         // load details about this page
        $anc = get_post_ancestors( $post->ID );
        foreach($anc as $ancestor) {
            if(is_page() && $ancestor == $pid) {
                return true;
            }
        }
        if(is_page()&&(is_page($pid))) 
                   return true;   // we're at the page or at a sub page
        else 
                   return false;  // we're elsewhere
    };
    
    add_action('get_header','ms_change_sidebar');
    function ms_change_sidebar() {
        
        if ( is_page( 'baza-wiedzy' ) || is_tree('18') ) { 
            remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); 
            remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); 
            add_action( 'genesis_sidebar', 'dodaj_sidebar_wiedza' ); 
        }
    }
    
  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Conditional Tag checkig if the parent of a page is a specific page doesn’t work’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble