Forums

Difference between these two exclusion conditionals? (4 posts)

  1. Seijun
    Member
    Posted 1 year ago #

    I have found two code snippets for excluding an element (in this case, the sidebar). Can anyone tell me what the difference is between them? The first one is always the one recommended, but the second one seems to do exactly the same thing.

    <?php if (is_page('1'))
    {}
    else {
    get_sidebar();
    }
    ?>
    <?php if (!is_page('1')) { ?>
    <?php get_sidebar(); ?>
    <?php } ?>
  2. Digital Raindrops
    Member
    Posted 1 year ago #

    Both exclude the sidebar if the page ID is one, however the first has a set of braces where you could do something else!

    Example switch the sidebar (version 3 with a template part)

    <?php if (is_page('1')) {
       /* get new sidebar in template part /*
       get_template_part( 'sidebar', 2 );
    } else {
       /* get the normal sidebar one */
       get_sidebar();
    }?>

    This one will leave a space where the sidebar should be on page ID one!

    <?php if (!is_page('1')) { ?>
       <?php get_sidebar(); ?>
    <?php } ?>

    HTH

    David

  3. Seijun
    Member
    Posted 1 year ago #

    So basically one just deletes the sidebar and the other also deletes the sidebar but also gives the option to replace it with something else..

  4. Digital Raindrops
    Member
    Posted 1 year ago #

    Not delete,just not load it!

    So basically both will not load the sidebar if the page ID is 1 and the the first code block is an 'if then else endif' statement where you can load something 'else' only if the page ID is 1 ..

    David

Topic Closed

This topic has been closed to new replies.

About this Topic