Can I get a “base URL” in WordPress within a template file?

百般思念 提交于 2019-12-03 06:28:48

问题


Usually in my PHP apps I have a base URL setup so I can do things like this

<a href="<?php echo BASE_URL; ?>tom/jones">Tom</a>

Then I can move my site from development to production and swap it easily and have the change go site wide (and it seems more reliable than <base href="" />.

I'm doing up a WordPress theme, and I am wondering, does WordPress have anything like this built in, or do I need to redefine my own?

I can see ABSPATH, but that is the absolute file path in the file system, not something from the document root.


回答1:


get_bloginfo('wpurl'); would be the preferred method of getting the base url of your WordPress installation. This always returns the absolute base url for the install where as get_bloginfo('url'); is for the actual blog address of your WordPress install.




回答2:


Yes, you can use get_bloginfo('url') just like that or define a constant...

define('BASE_URL', get_bloginfo('url'));

If you are working on a template and want the URL fragment to that theme folder, use...

bloginfo('template_directory'); 



回答3:


Yes you can get "base URL" with a simple function.

<?php echo get_bloginfo('url') ?>

after that with / you can reach to any page just type the page name.




回答4:


You can try using

<?php echo home_url(); ?>

By using this can get site url like www.xyz.com

<?php echo home_url('/contact'); ?>

By using this syntax you will get url like www.xyz.com/contact



来源:https://stackoverflow.com/questions/2848344/can-i-get-a-base-url-in-wordpress-within-a-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!