eiranix Posted June 27, 2014 Share Posted June 27, 2014 I want to use the php include function to apply the header navigation to each page of my site from a single file. How do you deal with pages that are within sub directories? For example, I have a News page that sits in the root folder but then all the news items are within a 'news' folder. This means all the relative nav links and image references on news item pages need '../' before them. Do I have to create a second menu file for subdirectories or is there a way around this? Link to comment Share on other sites More sharing options...
Don Jajo Posted June 28, 2014 Share Posted June 28, 2014 in my projects I do them like these: <?phpdefine('ABSPATH',dirname(__DIR__));?> or <?phpdefine('ABSPATH','/home/user/public_html/');?> When including i now use <?phpinclude(ABSPATH.'file.php');?> Link to comment Share on other sites More sharing options...
eiranix Posted June 28, 2014 Author Share Posted June 28, 2014 That looks helpful for the file you are including but what I meant was the links inside the file you are including. If the file you are including is the navigation menu, do I need to use a separate menu file when it's included on pages that are stored in a sub directory? In this example I want the menu included on all six pages, but 3 of them are in the folder called 'news'. This means the links in the menu won't work: /menu.php /home.php /news.php /contact.php /news/item1.php /news/item2.php /news/item3.php Or am I getting this wrong, does the included file base it's links off the location of the menu file or the location of the page the menu is included on? Link to comment Share on other sites More sharing options...
dsonesuk Posted June 28, 2014 Share Posted June 28, 2014 No you use just how you have been doing, adding include link relative to the document using ./menu.php ( which takes it out of news folder to the root), most people make the mistake of adding <html><body><head> with links to css and js files etc, but all is required is the actual menu html code, when this will used from the main document the menu is inserted in. Link to comment Share on other sites More sharing options...
eiranix Posted June 29, 2014 Author Share Posted June 29, 2014 Ok cool. So back to the original question - Is there a way using php or perhaps javascript to have the menu detect that it is currently being applied to a page in a sub directory and adjust the menu's links accordingly? Link to comment Share on other sites More sharing options...
dsonesuk Posted June 29, 2014 Share Posted June 29, 2014 (edited) in the menu links you include the full path with the first slash representing your root directory, when this is included in any pages, as long you start from root the links should tally with folder locations. <li><a href="/home.php">home</a></li> <li><a href="/contact.php">contact</a></li> <li><a href="/news.php">news</a></li> <li><a href="/news/item1.php">item1</a></li> <li><a href="/news/item2.php">item2</a></li> <li><a href="/news/item3.php">item3</a></li> if your root directory is localhost when you hover over links you would see 'localhost/home.php', 'localhost/news.php', 'localhost/news/item1.php Edited June 29, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
eiranix Posted June 29, 2014 Author Share Posted June 29, 2014 Ah ok great thanks. Is there a way to do this with image src as well as href? Link to comment Share on other sites More sharing options...
dsonesuk Posted June 29, 2014 Share Posted June 29, 2014 Same principle should apply as used in href. Link to comment Share on other sites More sharing options...
eiranix Posted June 29, 2014 Author Share Posted June 29, 2014 Yeah... scratch that, it didn't look like it worked to start with. Typo in my path. But thanks again it is just what I was looking for. I haven't used root-relative links before. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now