Forums

[resolved] subpage conditional statemement not working (22 posts)

  1. trixienolix
    Member
    Posted 10 months ago #

    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 } ?>'

  2. trixienolix
    Member
    Posted 10 months ago #

    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 } ?>
  3. ce_bradm
    Member
    Posted 10 months ago #

    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.

  4. trixienolix
    Member
    Posted 10 months ago #

    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??

  5. ce_bradm
    Member
    Posted 10 months ago #

    Hi trixienolix, If you haven't, I recommend you check out this page:
    http://codex.wordpress.org/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.

  6. trixienolix
    Member
    Posted 10 months ago #

    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] => http://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

  7. trixienolix
    Member
    Posted 10 months ago #

    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??

  8. alchymyth
    The Sweeper
    Posted 10 months ago #

    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'?

    http://codex.wordpress.org/Function_Reference/wp_reset_query

  9. ce_bradm
    Member
    Posted 10 months ago #

    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?

  10. trixienolix
    Member
    Posted 10 months ago #

    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!!

  11. ce_bradm
    Member
    Posted 10 months ago #

    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 ?

  12. alchymyth
    The Sweeper
    Posted 10 months ago #

    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 http://pastebin.com/ and post the link to it here, so someone could have a look at it?

  13. trixienolix
    Member
    Posted 10 months ago #

    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?

  14. trixienolix
    Member
    Posted 10 months ago #

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

  15. trixienolix
    Member
    Posted 10 months ago #

    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.

  16. ce_bradm
    Member
    Posted 10 months ago #

    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)?

  17. alchymyth
    The Sweeper
    Posted 10 months ago #

    try to follow: http://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query

    Note: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have Template Tags use the main query's current post again.

    i.e. possibly add <?php wp_reset_postdata(); ?> into line106.

    ---
    alternatively, try to save the $post object into a $temp variable before the extra loops, and then save it back after the loops;

    example:
    in line 10, add:
    <?php $temp = $post; //save original post object ?>
    and in line 106, add:
    <?php $post = $temp; //restore original post object ?>

  18. trixienolix
    Member
    Posted 10 months ago #

    ce_bradm: if you're on nuclear or biomass page (both of which are subpages of what we do - id 131) then you should see the content for that page, underneath that should be 2 relevant blog posts and then 2 relevant resources.

    alchymyth: I will have a go and report back

    I'm currently trying out a version with query_posts and reset...

  19. trixienolix
    Member
    Posted 10 months ago #

    reporting back...
    As suggested by alchymyth I added the code <?php wp_reset_postdata(); ?> into line 106 (i.e. after all of my multiple loop shenannigans) and...
    IT WORKED!
    Sidebars showing up correctly, knowing they are part of the tree of page 131, post_parent being recognised...
    wow, thank you.

    So, question:
    The codex says to do this if you use the_post() with your query. Is there a way to avoid using the_post() with WP_Query??
    Apologies if that is a daft question...

  20. ce_bradm
    Member
    Posted 10 months ago #

    Congrats trixienolix on fixing that!
    I'm not too familiar with wp_query, but the following page shows many other parameters that you can use with wp_query besides the_post:
    http://codex.wordpress.org/Class_Reference/WP_Query

  21. trixienolix
    Member
    Posted 10 months ago #

    I found this and it looks useful (but a bit over my head).. thought i'd put it here in case anyone needs it:
    wordpress query functions explained in diagram

  22. trixienolix
    Member
    Posted 10 months ago #

    thanks everyone. Really appreciate you helping me out.

Reply

You must log in to post.

About this Topic