Jump to content

If statement within an array


stefanlesik

Recommended Posts

Hi - I'm using the following code to print different the correct app link based on which device the user is viewing the page (using Wurfl). <a href="<?php echo $appLink ?>">Download the App link</a>

 if ($os == 'Android') { 
   $appName = 'Android app';
   $appLink = 'http://androidlink.com/1';
   
   } 
   else if ($os == 'iOS') 
   {  
   $appName = 'iPhone app';
   $appLink = 'http://iphonelink.com/1';
   } 

However, now I want display different links on different Wordpress pages using the same idea.

 if ($os == 'Android') { 
	if (is_page( 1 )) {
	$appLink = 'http://androidlink.com/1';
}
	if (is_page( 2 )) {
	$appLink = 'http://androidlink.com/2';
}
      
   } 

Is there a neat way of doing this?

 

Thanks!

Link to comment
Share on other sites

 

Try using arrays - 

$devices = array(
		'android'		=>		array(
			'name'			=>			'Android app',
			'link'			=>			'http://androidlink.com/1',
		),
		'ios'		=>		array(
			'name'			=>			'iPhone app',
			'link'			=>			'http://iphonelink.com/1',
		),
	);
	
	print $devices[$os]['name'] . ': ' . $devices[$os]['link'];

 

 

That has nothing to do with his issue and I question if you actually read the OP's post.

 

stefanlesik, I would ask what your real issue is apart from you not liking how your code is setup.

There is another method however, you can build your own custom array set storing the page id as a key and the value as the page url you want.

 

 <?php // This pseudo code would need to be replaced with your own setup.// The key is the page id while the value is the url you want to provide. $data = array(1 => 'first_route', 2 => 'second_route'); foreach($data as $page_id => $url){    if(is_page($page_id))    {        $appLink = 'http://site.com/' . $url;    }} 
 
Remember, is_page() accepts things other than the pages id such as the title or slug. e.g is_page('Homepage');
Link to comment
Share on other sites

Is there a function to get the active page id?

 

You can adapt the array method to this - 

<?php

	$devices = array(
		'android'		=>		array(
			'name'					=>			'Android app',
			'1'						=>			'http://androidlink.com/1',
			'2'						=>			'http://androidlink.com/2',
		),
		'ios'		=>		array(
			'name'					=>			'iPhone app',
			'1'						=>			'http://iphonelink.com/1',
			'2'						=>			'http://iphonelink.com/2',
		),
	);
	
	$page = (is_page( 1 )) ? 1 : 2; // Replace this with function to get active page
	
	print $devices[$os]['name'] . ': ' . $devices[$os][$page];

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.