WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] is_page -code with multiple pages in one? (4 posts)

  1. maisteriharju
    Member
    Posted 6 years ago #

    Topic may be confusing but I couldn't think of better one.

    I'm using conditional tags and is_page -function to get some code to some particular pages. I need to have different kind of layout on different pages so is_page -code seems to work perfectly for this.

    I've been doing it like this:

    <?php
    if (is_page('PageName1'))
    {
    echo "code here";
    }
    
    elseif (is_page('PageName2'))
    {
    	echo "same code here";
    }
    
    elseif (is_page('PageName3'))
    {
    	echo "different code here";
    }
    
    ?>

    As you noticed I have same code for some of the pages. I have so many pages that it's kind of frustrating to copy the elseif-lines many times and I started to wonder if there is some easier way. I should propably also mention that I'm not very familiar with this kind of coding as you may have already guessed.

    Is there any way to have many pages inside one is_page -code - like this:

    <?php
    if (is_page('PageName1', 'PageName2'))
    {
    echo "code here";
    }
    elseif (is_page('PageName3'))
    {
    	echo "different code here";
    }
    
    ?>

    It would be much easier. Is there AND or some similar code which could be used to make it work?

    Thanks for help.

    -TH

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    This way will work in WordPress 2.3.3:
    if (is_page('PageName1') || is_page('PageName2'))

    The || is an "or".

    Starting with WordPress 2.5, this will work:
    if (is_page(array('PageName1', 'PageName2')))

  3. maisteriharju
    Member
    Posted 6 years ago #

    Ok. I found out one simplier solution:

    <?php
    if (is_page('PageName1') || is_page('PageName2'))
    {
    echo "code here";
    }
    elseif (is_page('PageName3'))
    {
    echo "different code here";
    }

    ?>

    That's close enough.

  4. maisteriharju
    Member
    Posted 6 years ago #

    Thanks Otto42! We wrote the message at the same time. :) Array is even simplier way. Thanks for the help!

    - TH

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.