When I add links to pages in a custom WordPress theme I use the following function:
href="<?php echo esc_url(home_url( '/contact-page' ));?>"
If I don't use the home_ur()
function, but the following code, it still works:
href="./contact-page"
I'm currently only doing this on a localhost (MAMP) set up.
Is there any reason why I must use the home_ur()
function instead of just the HTML code, which will be quicker to type, and obviously quicker for the server/browser to process?
Emily.
When I add links to pages in a custom WordPress theme I use the following function:
href="<?php echo esc_url(home_url( '/contact-page' ));?>"
If I don't use the home_ur()
function, but the following code, it still works:
href="./contact-page"
I'm currently only doing this on a localhost (MAMP) set up.
Is there any reason why I must use the home_ur()
function instead of just the HTML code, which will be quicker to type, and obviously quicker for the server/browser to process?
Emily.
Share Improve this question asked Oct 11, 2019 at 12:43 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges 1 |1 Answer
Reset to default 1href="./contact-page"
- if you're on the homepage (
/
) this will become:/contact-page
- if you're on another page named foo (
/foo/
) this will become/foo/contact-page
This is due to ./
being a relative path. To avoid struggles like this, the method via home_url()
is preferred because it creates absolute links/paths that will work from anywhere you call it.
Note:
You could use href="/contact-page"
which is relative as well, but only to the domain and not the current page. However I can't tell you why WP rather uses and stores absolute URLs.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745089962a4610635.html
get_permalink
. Avoid hardcoded magic values – Tom J Nowell ♦ Commented Oct 11, 2019 at 12:56