WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Only Showing Elements on Homepage & Certain Pages (3 posts)

  1. ianbee
    Member
    Posted 1 year ago #

    Hi, I'm trying to get certain elements within my wordpress blog to only show on the front page, and other pages. I can successfully get items to only show on homepage by doing the following:

    <?php if( is_front_page() ) : ?>
    content...
    <?php endif;?>

    I can also show elements on just an array of pages by doing the following:

    <?php if(is_page(array(42,'bio','services'))) { ?>
    content...
    <?php } ?>

    I'm unsure how to show elements on both the front page and certain pages.. I tried the following but nothing worked...

    <?php if(is_page(array(42,'bio','services'))) { ?>
    <?php if( is_front_page() ) : ?>
    content...
    <?php } ?>

    I thought it could have been the "<?php } ?>" so I tried doing "<?php } ?><?php endif;?>" and also just "<?php endif;?>"

    Again, nothing worked.

    So how do I make certain elements only appear on the front page AND certain pages?

  2. Peter_L
    Member
    Posted 1 year ago #

    Use boolean operators:
    or -> ||
    and -> &&

    In your case, you need the or operator. You want to do something if it's a certain page OR the front page. Looks like this:

    <?php
    if( is_front_page() || is_page(array(1,2,3)) ){
         //do something
    }
    ?>
  3. ianbee
    Member
    Posted 1 year ago #

    @Peter_L I tweeked the code a bit and it worked! thanks so much man! :D

Topic Closed

This topic has been closed to new replies.

About this Topic