Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

How would you combine these two wordpress/php if statements? I'm loading the flexslider css on home page and work posts.

<?php
// Is Sub_Page function to find subpages
function is_subpage() {
    global $post;                              // load details about this page

    if ( is_page() && $post->post_parent ) {   // test to see if the page has a parent
        return $post->post_parent;             // return the ID of the parent post

    } else {                                   // there is no parent so ...
        return false;                          // ... the answer to the question is false
    }
}

function theme_styles() {
    wp_register_style( 'flexslider', get_template_directory_uri() . '/css/flexslider.css');

    if( is_page( 'home' ) ) {
        wp_enqueue_style( 'flexslider' );
    }   
    if( is_page('work') == is_subpage() ) {
        wp_enqueue_style( 'flexslider' );
    }   
}

?>

I tried to this but it didn't work:

<?php

if ( is_page( 'home' ) || is_page( 'work' ) == is_subpage() ) 

?>

Timothy,

Does your 2 if statements work correctly and you're simply trying to refactor the code to make it simpler/shorter?

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Tim, try using :

<?php

if ( is_page( 'home' ) || (is_page( 'work' ) == is_subpage() )  ) 

?>

It probably won't make a difference, but give it a shot. If that does work add this to your theme_styles function.

<?php
        var_dump( is_front_page() ); //use this if you want the site's home page or use is_home() if you want the site's blog posts index page 
        var_dump( is_page( 'work' ) == is_subpage() ) ;
?>

Go to the pages and see what boolean values are returned. It should help you find out which condition is faliing.

Hi Andrew,

I think it shouldn't make a difference since the comparison operator has higher precedence than the logical OR operator.

Andrew Shook
Andrew Shook
31,709 Points

Yeah I know, but a sanity check is always good.