Hello, I need to ad a menu link to the homepage's navigation ONLY - and not have that link sitewide. How do I check via PHP if the page loaded is the homepage URL, and if so, generate that link? If the page loaded is a sub-page, then that link should NOT be there. EXAMPLE: Homepage + this link Sub-page w/o link I need this PHP code to be within an included sidebar.php file that sits in a sub folder: /includes/sidebar.php Code examples would be very appreciated!
you can try using: <? $homepage = "/index.php"; $currentpage = $_SERVER['REQUEST_URI']; if($homepage==$currentpage) { echo "links links links links"; } ?> Code (markup):
I'm using this, but it does NOT work: <!-- begin homepage ads --> <?php $homepage = "/index.php"; $currentpage = $_SERVER['REQUEST_URI']; if($homepage==$currentpage) { echo '<!-- begin link-->'; echo '<li><a href="http://www.domain.com/" rel="external">Homepage only link</a></li>'; echo '<!-- end link -->'; } ?> <!-- end homepage ads --> PHP: It doesn't put that link on the page (or any page). Did I do something wrong?
try putting echo $currentpage; , put it right after the $currentpage =, line and goto your homepage to see what it displays.
Yes - that does echo the current location for whatever page I'm on. It shows / - /sub-folder/ - /etc/ So it does "know" the URL location, but it just is NOT echoing the link tag
I just found something... On my local development PC (running Apache/PHP) - that bit of code DOES work!!!! Just NOT working on my host...
Check the value of $currentpage by adding a debug line: echo $currentpage. If it does have a value there for when you visit the homepage try making $homepage that value assuming it's correctly returning the homepage for you. If it's not returning anything, it may be that your PHP installation doesn't support REQUEST_URI and you'll have to check your php settings to see what SERVER vars you can use. Check those out using phpinfo().
On my local PC, yes it does. On my hosting account, no... Got it: And yes, it shows it's enabled: REQUEST_URI /phpinfo.php Code (markup):
I'm assuming then that you're homepage is at your domain rather than a page such as somedomain.com/. You'll have to check then if $currentpage = '' or index.php
I believe the syntax is correct, as it DOES work on my local apache/php server. Just not on my web host... So, when I use the code listed above, my local apache server serves the ad on the homepage only, and sub-pages do NOT get it. On the hosting account however, all I get is the echo of the current URI...
No, definitely syntax is correct. If you're navigating to the homepage via the main domain and not /index.php it's not going to match. If the $currentpage that is being echoed is different from what you have set to $homepage, just change it to that.
Works!!! Here's the total code: <!-- begin homepage ads --> <?php $homepage = "/"; $currentpage = $_SERVER['REQUEST_URI']; if($homepage==$currentpage) { include('homepage-ads.php'); } ?> <!-- end homepage ads --> PHP: Must have been that /index.php - that I changed to just / You guys rock!
I would like to do something similar to this however I would like to display a different title tag depending on the directory. Basically so that I can use the same theme for two different programs. <?php $directory = "/directory/...."; $currentpage = $_SERVER['REQUEST_URI']; if($directory==$currentpage) { echo "<title>{SITETITLE}</title>"; }else{ echo "<?php wp_title(); ?></title>"; ?> Code (markup): It would need to display <title>{SITETITLE}</title> Code (markup): for any page which exists in the /directory sub directory. I hope that makes sense? Cheers, Mike.
I am sure you would just have to load the current URL into an array and if the first 10 number of characters match /directory then display <title>{SITETITLE}</title> Am I on the right track?
<!-- begin homepage ads --> <?php $currentpage = $_SERVER['REQUEST_URI']; if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) { include('homepage-ads.php'); } ?> <!-- end homepage ads --> PHP: to make it work in any condition www.yourdomain.com & www.yourdomain.com/ & www.yourdomain.com/index.php