stefanlesik Posted January 27, 2014 Share Posted January 27, 2014 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! Quote Link to comment Share on other sites More sharing options...
adam_bray Posted January 27, 2014 Share Posted January 27, 2014 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']; Quote Link to comment Share on other sites More sharing options...
0xMatt Posted January 27, 2014 Share Posted January 27, 2014 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'); Quote Link to comment Share on other sites More sharing options...
stefanlesik Posted January 27, 2014 Author Share Posted January 27, 2014 Hi Adam Thanks fo ryour reply. How would I display a different link on different pages with your method as per my example? Thanks Quote Link to comment Share on other sites More sharing options...
adam_bray Posted January 27, 2014 Share Posted January 27, 2014 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]; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.