Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with if statement WordPress

  • SOLVED

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 } ?>

Answers (7)

2013-01-30

Arnav Joy answers:

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>';

}

2013-01-30

Navjot Singh answers:

Remove Single quotes from around your page ids.


Ross Gosling comments:

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?

2013-01-30

Kiet Luong answers:

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.


Kiet Luong comments:

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.


Ross Gosling comments:

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

2013-01-30

Manoj Raj answers:

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)) {


Ross Gosling comments:

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>';
}
?>


Manoj Raj comments:

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>';
}
?>





2013-01-30

Monit Jadhav answers:

Try adding

global $post;

Before the loop and condition


Monit Jadhav comments:

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 } ?>



Monit Jadhav comments:

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.
}

?>


Monit Jadhav comments:

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; ?>

2013-01-30

Abdelhadi Touil answers:

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:
[[LINK href="http://codex.wordpress.org/Function_Reference/is_page"]]http://codex.wordpress.org/Function_Reference/is_page[[/LINK]]

2013-01-30

Francisco Javier Carazo Gil answers:

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>';

}

?>


Francisco Javier Carazo Gil comments:

Or directly, make another function:


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

return $false;
}