• Resolved bogdandimitrov

    (@bogastyle)


    Hi WordPress people!
    I’m new in WordPress! This days I started to build my first WordPress theme!
    I whant to show 2 columns layout when right sidebar is active or when current page has child pages or child page is active, because links for subpages are visible in right sidebar.
    In page.php I have code like this:

    <?php if ( is_active_sidebar( 'right-sidebar' ) or $post->post_parent == true) : ?>
    ...Show 2 coloumns...
    <?php else : ?>
    ... Show 1 coloumns ...
    <?php endif; ?>

    But this check for $post->post_parent obviously is wrong!
    Can some body help me, please!

    Thanks in advice!
    I hope U’ll understand my bad English! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • Try:

    false != $post->post_parent

    I’m pretty sure that, when TRUE, $post->post_parent returns an ID, rather than TRUE.

    Thread Starter bogdandimitrov

    (@bogastyle)

    Thanks Chip,
    but this not work, too!

    First, upon further investigation, it appears that if ( $post->post_parent ) should work.

    Also, I think you have a PHP boolean syntax error.

    This:

    <?php if ( is_active_sidebar( 'right-sidebar' ) or $post->post_parent ) : ?>

    should instead be this:

    <?php if ( is_active_sidebar( 'right-sidebar' ) || $post->post_parent ) : ?>

    In other words, use the boolean operator || instead of or.

    Thread Starter bogdandimitrov

    (@bogastyle)

    Thanks Chip for the correction and the proposal! But still does not work!
    here’s the entire code

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Hmm… re-reading your original post, your second conditional is intended to be true if the current Page has children, right?

    In which case, $post->post-parent won’t get you there, because it indicates that the current Page is a child Page.

    Try instead using get_pages(): http://codex.wordpress.org/Function_Reference/get_pages

    e.g.:

    <?php if ( is_active_sidebar( 'right-sidebar' ) || get_pages( array( 'child_of' => $post->ID ) ) ) : ?>

    (Edited to correct get_pages() argument)

    Thread Starter bogdandimitrov

    (@bogastyle)

    Hi Chip,
    U are WordPress nindja!

    <?php if ( is_active_sidebar( 'right-sidebar' ) || get_pages( array( 'child_of' => $post->ID ) ) ) : ?>

    That is work for parent page!

    I add code from previews post, like this:

    <?php if ( is_active_sidebar( 'right-sidebar' ) || get_pages( array( 'child_of' => $post->ID ) ) || $post->post_parent) : ?>

    And now it’s work for parent and child pages, too!

    Thanks again!
    Best Regards!

    Glad to help! 🙂

    (p.s. be sure to mark the topic as “Resolved” to facilitate others with similar issues finding a solution.)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to check for parent page’ is closed to new replies.