The Wordpress way URL on static / dynamic pages - php

I would like to add a menu or footer like in Wordpress or any other CMS.
That really dont work with PHP includes because if a website file is in subdirectory there is a problem with the path.
As you know a normal static menu looks like
<nav>
<ul>
<li>About</li>
</ul>
</nav>
but CMS menu looks like
<nav>
<ul>
<li>About</li>
</ul>
</nav>
And that on all pages so no problems with the path
if you try it with static/dynamic pages you have to add the path or the url and that looks really ugly or not
<?php
define('WEB_ROOT', '../'); // relative path to /
?>
...
<nav>
<ul>
<li>Solution</li>
or
<li>Solution</li>
or
<li>Solution</li>
</ul>
</nav>
I think thats a bad idea and really ugly.
so how to solve that kind of problem maybe with adding a web root in php like above but looks not really good so do you have any other ideas?
And that Solution should also work for CSS and JS not only for static contents, so all styles are same and menus looks just like the other pages even if it is in the subdirectories

Generally in Wordpress I use the site url to generate full urls:
site_url('/about');
As for JS/CSS files, it depends where you are putting them. If the files are located in a specific theme use:
get_template_directory();

you can use it...in "a" tag href="", use this:
echo home_url('index.php/about');
or remove the index.php, just about [the name of the page.]
Done.

Related

Image source path changing on different pages

I'm using Genesis and I have a functions.php that I have inserted images there using hooks to the footer.
I've used a widget to insert an image to the header.
The images are displayed correctly when viewed in the homepage, but when I switch a page, the images are not displayed.
Further investigation concludes that wordpress is seeking the file with a wrong path source.
That's the path source in the homepage - wp-content\themes\childTheme-Almog\images\linkedin.png
And wordpress is looking for it as it should.
However, on another page, called 'X', wordpress is looking for the same image as follows:
\wordpress\x\wp-content\themes\childTheme-Almog\images\linkedin.png
As you can tell, it adds to the path the page name as a folder, which it shouldn't do, because the image is not there.
Is there a way to make wordpress look for the image like it does on the homepage?
Here's how my images are implemented in my functions.php:
<div class="d_footer">
<ul id="list_left">
<li class="foot" id="ft_text1"><img src="wp-content\themes\childTheme-Almog\images\phone.png" alt="phone">
<span style="color: #969697">|</span><span style="margin-left:30px;"></span><img src="wp-content\themes\childTheme-Almog\images\envelope.png" alt="envelope"></li>
<li class="foot" id="ft_text2"></li>
</ul>
<ul class="foot" id="list_right">
<li id="ft_text3"><img src="\wp-content\themes\childTheme-Almog\images\linkedin.png" alt="linkedin" align="left">Almog's linkedin profile</li>
<li id="ft_text4"></li>
</ul>
</div>
You really should be using Wordpress' get_stylesheet_directory_uri variable to alleviate problems with paths like this.

Static Menu on all Subpages

I am creating a HTML/PHP based Website. Now I would like to add a menu but that menu should have on all pages the same structure like on a CMSso I mean:
On a normal/static dynamic website with subdirectories you have following structure
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
but if it a php file is on a subdirctoryas solution
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
On a normal CMS you see on all page also on pages witch are in 'subdirectories'
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
I think with you can easy manage and change that kind of menu and you donnt have to change a mistake on 50 pages:)
I think I could do it with PHP but dont have any idea.
Maybe with Base URL but the the url would look like
<li>Contact</li>
and thats not that what I mean and want to get.
Hope you could give me an advice how to realize that
It really depends on the CMS of your choice. For Wordpress for example to establish base reference you use something like Your Page. If you later decide that sample page need to be called page-new for example I would go with find and replace function of any good editor out there. It will take certainly time for 50 or more links but it will be significantly less.

Website Navigation within the Header

I am having trouble properly setting up navigation on my website. I am putting the navigation into my header and I want to get all of the links to work no matter what layer in the file structure that the webpage is on.
This is my file structure:
Website (directory)
index.php
resources (directory)
includes (directory)
html_codes.php
Game (directory)
game_info.php
resources
Characters (directory)
characters_info.php
Players (Directory)
players_info.php
Inside the html_codes.php is a function that builds the header for the website. I use this function to create the header at the top of all of my web pages. However the hyperlinks that navigate around the website do not work in any layer except the top-level directory because their relative position has changed.
Is there a way to get the links to work in
./index.php
./game/game_info.php
./game/Characters/characters_info.php
./game/Characters/Players/players_info.php
from the same create_header() function?
My create_header() function:
function create_Header(){
echo '
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
';
}
*********************************EDIT***************************************
Okay, I used your suggestions and it solved some problems and created others.
First off, turning the paths from relative to absolute worked but it required that I change the path names to
/Website/index.php
/Website/game/game_info.php
/Website/game/Characters/characters_info.php
/Website/game/Characters/Players/players_info.php
which is fine but I don't understand why. I assume it is because the Website directory is a sub-directory for C:\xampp\htdocs\Website.
The other issue is that my include functions don't work with this absolute path.
The relative path for the include inside of game_info.php before was
include("./../includes/html_codes.php");
which did and still does work.
I have tried both
include("/includes/html_codes.php");
include("/testphp/includes/html_codes.php");
and they did not work.
Two things
1. i recommend not making capitalized folder names in your source and in this header. otherwise some browsers may require the capitalization by your users. which isn't really standard.
2. i recommend you use absolute paths (removed .)
function create_Header(){
echo '
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
';
}
Put a forward slash / in front of your paths to make them absolute.
Go home
This will ensure that you're always starting from the same place (root), and that your links are independant from the hierarchy.
If your website is live, you can use an absolute URL like this one :
Go home
./ is relative to the current directory. There are two ways to solve it. The first one: keep using relative paths, but let them move up as well. So if you are on ./game/Characters/characters_info.php, the directory is ./game/Characters/, and the path to home is ../../index.php.
So you'll need to know not only the depth of the current page, you'll need to get up to a level that is shared between the current page and the target page. After all, both could be two levels deep, but still are in a different directory.
I think a better (at least much easier) way is to use an absolute path. To do that, you can just start with a / to have the absolute path from the domain name.
Optionally, you could configure an 'installation path' or 'base directory' to start each path with. It could default to just '/', but you could set it to another value if the site was entirely in a subdirectory.
So the header would look like:
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
Or, with a base path variable, you can move the site to another directory, just change the variable, and everything will still work. Very convenient for testing and development. The normal value for this variable can just be ''.
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>

Navigation menu A Href doesn't work when back to directory try?

I tried to make the navigation menu. I took his menu file using code "include" in my index.php file.
When I select the guitar work. but when re-pressing the home button. can not go back to the index or home? guitar.php files are in direcorty pages / guitar.php.
Why this can not go back to the index directly? please help me. Thank you
<ul>
<li><a href="index.php" >Home</a></li>
<li>Guitars
<ul>
<li>Guitar Acoustics</li>
<li>Guitar Electirs</li>
<li>Guitar Amplifiers</li>
<li>Guitar Effects</li>
</ul>
</ul>
above is the code in the file navigation menu.
<?php
require_once "layout/header.php";
include ("layout/nav.php");
require_once "layout/aside.php";?>
?>
above is the code in file index.php, and pages/guitar.php.
Thanks ...
Your links are relative, which means they'll just replace the "xxx.php" bit at the end of your URL, so when you go to "pages/guitar.php" your home link becomes "pages/index.php".
Make them start with a / and they'll be relative to your domain, eg. if your site is mydomain.com, your homepage becomes mydomain.com/index.php and the guitars page is mydomain.com/pages/guitar.php.
Home
Guitars
If gets a shade more complicated if your site isn't at the root of your domain. For example, if your homepage is actually at mydomain.com/guitar-site/index.php then you'll need to add the /guitar-site to the front of the URLs as well, so the link goes to the right place:
Home
Guitars
Try replacing include ("layout/nav.php"); with header("location:layout/nav.php");

Dynamic Navigation?

Currently for my projects I create the navigation for each page manually and it looks something similar to:
<nav>
<ul id="mainMenu"><!--Main Menu-->
<li><a class="active" href="index.php">Home</a></li>
<li>Contact</li>
</ul>
</nav>
This works fine, however for projects that has many many pages it is not a really good practice or even efficient to do it manually. So I was wondering if there is anyone who can direct me to the right path and advice me on how to make my navigation dynamic? I know about PHP include and the .inc files - they are good. BUT I want to add class .active to the <a> of the page that is currently open. How can I do that?
BTW: I don't know if this s the right place to post this sort of questions here, but the moderator told me to post it here.
Use include to add a central php file, that contains a function which can take the current page as a parameter:
nav.inc:
function renderNavigation($current_page) {
//render you navigation
}
main.php:
require_once("nav.inc");
renderNavigation("Subpage 1")

Categories