• Resolved trixienolix

    (@trixienolix)


    I need to code the sidebar with a conditional statement so that if you add a new page that is a subpage of page 131 (“what we do”) then the “what we do” sidebar will show.

    it’s not working. Any ideas?

    ‘<?php if ( is_page( ‘what-we-do’ ) || ‘131’ == $post->post_parent ) { ?>
    <?php if ( ! dynamic_sidebar(‘What We Do Sidebar’) ) : ?><?php endif; ?>
    <?php } else { ?>
    <?php if ( ! dynamic_sidebar(‘Main Sidebar’) ) : ?><?php endif; ?>
    <?php } ?>’

Viewing 15 replies - 1 through 15 (of 21 total)
  • Thread Starter trixienolix

    (@trixienolix)

    sorry, code not in backticks again:

    <?php if ( is_page( 'what-we-do' ) || '131' == $post->post_parent ) { ?>
    <?php if ( ! dynamic_sidebar('What We Do Sidebar') ) : ?><?php endif; ?>
    <?php } else { ?>
    <?php if ( ! dynamic_sidebar('Main Sidebar') ) : ?><?php endif; ?>
    <?php } ?>

    It was hard to understand your code, so I cleaned it up a little and am looking at this:

    <?php
    	if ( is_page( 'what-we-do' ) || '131' == $post->post_parent )
    	{
    		if ( ! dynamic_sidebar('What We Do Sidebar') ) : endif;
    	}
    	else
    	{
    		if ( ! dynamic_sidebar('Main Sidebar') ) : endif;
    	}
    ?>

    What I would do is print out some variables before this in attempt to see what’s going wrong, such as:

    <?php
    	echo "<p>is page 'what-we-do' = " . is_page( 'what-we-do' ) . "</p>";
    	echo "<p>post parent = " . $post->post_parent . "</p>";
    ?>

    I hope this helps. Let us know what respond you get with this testing.

    Thread Starter trixienolix

    (@trixienolix)

    Thank you for looking.
    I pasted your code in to my sidebar.php above my (now cleaned up!) code and this is what I get:

    is page ‘what-we-do’ =
    post parent = 0

    this was on a subpage of “what-we-do”.

    On the actual what-we-do page i get this:
    is page ‘what-we-do’ = 1
    post parent = 0

    and the correct sidebar shows up.

    So it’s not picking up my page parent?

    If i look at my rendered source code, my body tag should give me some more clues:
    <body class="page page-id-146 page-child parent-pageid-131 page-template-default logged-in admin-bar single-author singular">
    which looks like it is seeing the parent??

    Hi trixienolix, If you haven’t, I recommend you check out this page:
    https://codex.www.ads-software.com/Conditional_Tags

    It has a function named is_tree() (which you’ll need to copy / past to your functions.php file) that seems to do what you’re looking to do:

    Find the section on the page similar to:

    Add Snippet 4 to your functions.php file, and call is_tree( ‘id’ ) to see if the current page is the page, or is a sub page of the page. In Snippet 3, is_tree( ‘2’ ) would replace “is_page( ‘about’ ) || ‘2’ == $post->post_parent” inside the first if tag.

    On a side note, you may also want to take a peek at all the values of page, by doing this:
    <pre><? print_r($post); ?></pre>
    You may find another value that is = to 131 that you can use.

    If non of this helps, please let me know and I’ll get my hands wet and see if we can figure this one out.

    Thread Starter trixienolix

    (@trixienolix)

    thanks for your help. Reall appreciated.

    I added your print_r code to my page (for anyone else that needs it, the actual code you need to put in the page is this: <?php print_r($post); ?>

    On my subpage i get this:

    stdClass Object ( [ID] => 146 [post_author] => 1 [post_date] => 2011-07-26 12:44:02 [post_date_gmt] => 2011-07-26 12:44:02 [post_content] => CONTENT REMOVED FOR THIS FORUM [post_title] => Nuclear Energy [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => nuclear-energy [to_ping] => [pinged] => [post_modified] => 2011-07-26 14:55:32 [post_modified_gmt] => 2011-07-26 14:55:32 [post_content_filtered] => [post_parent] => 131 [guid] => https://XXX/?page_id=146 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 [ancestors] => Array ( [0] => 131 ) [filter] => raw )

    I then added snippet 4 to my functions .php and altered my sidebar code to this (i added some code to test the tree code as well – see below):

    <?php
    	echo "<p>is page 'what-we-do' = " . is_page( 'what-we-do' ) . "</p>";
    	echo "<p>post parent = " . $post->post_parent . "</p>";
    	echo "<p>is tree = " . is_tree( '131' ) . "</p>";
    ?>
    
    <?php
    
    	if ( is_tree( '131' ) )
    	{
    		if ( ! dynamic_sidebar('What We Do Sidebar') ) : endif;
    	}
    	else
    	{
    		if ( ! dynamic_sidebar('Main Sidebar') ) : endif;
    	}
    ?>

    When I’m on my page 131 I get the correct sidebar and the following info about it:

    is page ‘what-we-do’ = 1
    post parent = 0
    is tree = 1

    But when I’m on my subpage of 131 (the nuclear energy page listed above I get this:

    is page ‘what-we-do’ =
    post parent = 0
    is tree =

    even thought it is a subpage of page 131!

    SO now I’m wondering if one of my multiple loops on the page is doing something odd to my conditional statements? I struggle with multiple loops so there’s a chance i’ve not done it right. Would that have this effect?

    thank you in advance

    Thread Starter trixienolix

    (@trixienolix)

    ok, it’s definitely something to do with teh code on my page.php – if i just have 1 loop it works fine (sidebar shows correct tree and parent info) but when I put my loops back in it goes wrong.

    Could you help with my conditional statements and multiple loops??

    when I put my loops back in it goes wrong.

    have you tried to add wp_reset_query(); after these loops to bring the query_string back to ‘normal’?

    https://codex.www.ads-software.com/Function_Reference/wp_reset_query

    Hi trixienolix. The post parent variable is working for me in my tests. I edited the loop-page.php file. Which specific file are you editing, or is this a plugin that you’re editing?

    Thread Starter trixienolix

    (@trixienolix)

    i’ve tried everything! I’ve used query_posts with a reset and now I’m using new WP_Query…
    whenever i put a secondary loop onto my page, the sidebar loses the ability to check the tree or parent status…
    I’m going to keep trying as it must just be something daft that I’m doing.
    aaagggghhh!!

    I don’t understand why you’re running a loop. Are you instead referring to the conditional statements (if / then) ?

    Also, are you editing sidebar.php ?

    whenever i put a secondary loop onto my page, the sidebar loses the ability to check the tree or parent status…

    would you like to paste the full code of that page into a https://pastebin.com/ and post the link to it here, so someone could have a look at it?

    Thread Starter trixienolix

    (@trixienolix)

    can you advise me on which methos of adding multiple loops is best? There are many different examples out there, some using query_posts with a reset and others using wp_query. What are the advantages / differences of each method?

    Thread Starter trixienolix

    (@trixienolix)

    just seen your comment – will put the page back together and put it up
    thanks you so much

    Thread Starter trixienolix

    (@trixienolix)

    OK, here’s the original code that was messing up my sidebar tree and post_parent:

    messes up sidebar conditionals
    There are multiple loops and conditional statements.
    If you could let me know what’s wrong with it i’d be really greateful. My knowledge of multiple loops is obviously lacking.

    Is “Nuclear” suppose to be a category or a page?

    If you’re on “Nuclear”, to confirm, the goal is to display links to 2 sub pages (or posts)?

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘subpage conditional statemement not working’ is closed to new replies.