logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Help with if statement

Hello, could someone please help me with the following statements.

I'm trying to show a certain link if the page is either ID 10, 624, 633, 725 otherwise show other link. I've had a go but didn't get it working, not sure what one to use: (this will go in my page.php)


<?php if ( is_page(array('10','624','633','725')) ){
echo '<a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';
} else {
echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>';
}
?>



<?php if ( is_page(array('10','624','633','725')) ){ { ?>
<a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>
<?php } else { ?>
<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>
<?php } ?>

This question has been answered.

Ross Gosling | 01/30/13 at 2:52pm Edit
Tutorial: How to assign prize money


(16) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/30/13
    2:57pm
    Navjot Singh says:

    Remove Single quotes from around your page ids.

  • avatar
    Last edited:
    01/30/13
    3:01pm
    Kiet Luong says:

    Cannot Be Used Inside The Loop
    Due to certain global variables being overwritten during The Loop is_page() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.

  • avatar
    Last edited:
    01/30/13
    3:10pm
    Manoj Raj says:

    You can try the following

    Get your page id

    and use in_array php function

    Example

    if (in_array($yourpageid, array(10,624,633,725), false)) {

  • avatar
    Last edited:
    01/30/13
    3:20pm
    Monit Jadhav says:

    Try adding

    global $post;

    Before the loop and condition

  • avatar
    Last edited:
    01/30/13
    3:29pm
    Abdelhadi Touil says:

    For me, in such situation I don't like to use "echo", so I use like this code:

    <?php if ( is_page(array(10, 624, 633, 725)) ){ ?>
    <a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>
    <?php } else { ?>
    <a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>
    <?php } ?>


    Also don't put pages ID between single quotes.
    Reference:
    http://codex.wordpress.org/Function_Reference/is_page

  • avatar
    Last edited:
    01/30/13
    3:57pm
    Francisco Javier Carazo Gil says:

    You can do it:



    <?php if ( is_page('10') || is_page('624') || is_page('633') || is_page('725')){

    echo '<a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';

    } else {

    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>';

    }

    ?>

  • avatar
    Last edited:
    01/30/13
    5:43pm
    Arnav Joy says:

    try this

    <?php 



    if ( in_array( get_the_ID(), array( 10,624,633,725 ) ) ) {

    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';

    } else {

    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/register/" title="Register"><div id="registerbutton"></div></a>';

    }

  • avatar
    Last edited:
    01/30/13
    3:07pm
    Ross Gosling says:

    Hi thanks for your quick answer, I got this to work:


    <?php if ( is_page(array(fixed-income,investments,benefits)) ){
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';
    } else {
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>';
    }
    ?>


    But for some reason would not work with page IDs, do you know why?

  • avatar
    Last edited:
    01/30/13
    3:08pm
    Kiet Luong says:

    is_page() function

    Cannot Be Used Inside The Loop
    Due to certain global variables being overwritten during The Loop is_page() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.

  • avatar
    Last edited:
    01/30/13
    3:09pm
    Ross Gosling says:

    Thanks for your answer, how would I do that then?

  • avatar
    Last edited:
    01/30/13
    3:21pm
    Ross Gosling says:

    I tried this, but did not work, is this correct?


    <?php if (in_array($yourpageid, array(10,624,633,725), false)) {
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';
    } else {
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/register/" title="Register"><div id="registerbutton"></div></a>';
    }
    ?>

  • avatar
    Last edited:
    01/30/13
    3:26pm
    Monit Jadhav says:

    Hello mate

    this code should work


    <?php if ( is_page(array(10,624,633,725)) ){ { ?>

    <a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>

    <?php } else { ?>

    <a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>

    <?php } ?>


  • avatar
    Last edited:
    01/30/13
    3:28pm
    Manoj Raj says:

    How did you get your page id?

    Try this code

    <?php 
    global $wp_query;
    $mypage_id = $wp_query->get_queried_object_id();
    if (in_array($mypage_id, array(10,624,633,725), false)) {
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';
    } else {
    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/#/register/" title="Register"><div id="registerbutton"></div></a>';
    }
    ?>





  • avatar
    Last edited:
    01/30/13
    3:32pm
    Monit Jadhav says:

    oops i added an extra curly bracket


    <?php if ( is_page(array(10,624,633,725)) ){

    echo '<a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>';

    } else {

    echo '<a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>';


    this is the correct code. Basically write page ID as numbers i.e. not in quotes and it should work. Let me know if there is trouble.
    }

    ?>

  • avatar
    Last edited:
    01/30/13
    3:35pm
    Monit Jadhav says:

    More refined code would be, this is more readable and no need to echo.

    <?php if ( is_page(array(10,624,633,725)) ): ?>

    <a id="registerbuttonlink" http://www.pinecommodities.co.uk/#/storepod-register/" title="Storepod Register"><div id="registerbutton"></div></a>

    <?php else: ?>

    <a id="registerbuttonlink" href="http://www.pinecommodities.co.uk/register/" title="Register"><div id="registerbutton"></div></a>

    <?php endif; ?>

  • avatar
    Last edited:
    01/30/13
    3:58pm
    Francisco Javier Carazo Gil says:

    Or directly, make another function:


    function is_page_array($array)
    {
    foreach($array as $value)
    if(is_page($value))
    return $true;

    return $false;
    }

This question has expired.



Gabriel Reguly, Ross Gosling voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.