Community Forums › Forums › Archived Forums › General Discussion › Conditional Tag checkig if the parent of a page is a specific page doesn’t work
Tagged: conditional tag, parent of a page
- This topic has 2 replies, 2 voices, and was last updated 6 years ago by
misionis.
-
AuthorPosts
-
February 15, 2017 at 8:07 am #201203
misionis
ParticipantHello 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 #201261Victor Font
ModeratorThe 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 #203141misionis
ParticipantThank 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' ); } }
-
AuthorPosts
- The topic ‘Conditional Tag checkig if the parent of a page is a specific page doesn’t work’ is closed to new replies.