links - Using Static HTML instead of the `home_url()` WP function

When I add links to pages in a custom WordPress theme I use the following function:href="<?php echo esc_url(home

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
  • 2 You shouldn't hard code URLs like that, instead you should use a nav menu, or refer to the pages post ID with get_permalink. Avoid hardcoded magic values – Tom J Nowell Commented Oct 11, 2019 at 12:56
Add a comment  | 

1 Answer 1

Reset to default 1
href="./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

相关推荐

  • links - Using Static HTML instead of the `home_url()` WP function

    When I add links to pages in a custom WordPress theme I use the following function:href="<?php echo esc_url(home

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信