WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] condition staments help with excluding pages (9 posts)

  1. oldbag66
    Member
    Posted 5 years ago #

    i want to show content on all pages except on in the side bar, im usinh conditional statements but cant seem to get it right, here is my php:

    <?php if (is_page()) { ?>
    <p>something for all other pages goes here</p>
    <?php } elseif (is_page('37')) { ?>
    <p>something for page 37 only goes here</p>
    <?php } ?>
  2. Josh Leuze
    Member
    Posted 5 years ago #

    The problem is that the first statement, "is_page", applies to all pages, including page 37, so it always uses the first statement.

    You want to reverse the order, so that you conditions start out specific, and then become more general.

    Also, you only need an "if" and "else" for two conditions:

    <?php
     if ( is_page('37')) {
     echo "<p>something for page 37 only goes here</p>";
     }
     else {
     echo "<p>something for all other pages goes here</p>";
     }
     ?>
  3. oldbag66
    Member
    Posted 5 years ago #

    thats great, im just learning about the conditionals, thanks

  4. Josh Leuze
    Member
    Posted 5 years ago #

    No problem!

  5. oldbag66
    Member
    Posted 5 years ago #

    another similar problem im trying to get a conditional statement to apply to certain pages ie:

    <?php } else if (is_page('46','49','51','53')) { ?>

    cant get it to work or find info on this, i only want something to show on these pages.

  6. Josh Leuze
    Member
    Posted 5 years ago #

    It won't work on its own like that. The conditional statement needs to go if/else or if/elseif/else.

  7. oldbag66
    Member
    Posted 5 years ago #

    so you cant apply it to multiple pages at the same time rather than writing if else for each page

  8. Josh Leuze
    Member
    Posted 5 years ago #

    You can apply it to as many pages as you want, but the conditions need to be ordered properly.

    This isn't going to do anything because there isn't an if to start the conditions, or an else to end them:

    <?php } else if (is_page('46','49','51','53')) { ?>

    If you want to have one page have code A, and then several pages have code B, and all the rest have code C, you want to start with the most specific, a single page, then the more broad, several pages, then the catch-all, like this:

    <?php
     if ( is_page('37')) {
     echo "<p>something for page 37 only goes here</p>";
     }
     elseif ( is_page('46','49','51','53')) {
     echo "<p>something for pages 46, 49, 51, and 53 goes here</p>";
     }
     else {
     echo "<p>something for all other pages goes here</p>";
     }
     ?>

    Does that make sense?

  9. oldbag66
    Member
    Posted 5 years ago #

    that does thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic