如何获得当前页面的URL显示?
-
-
您能否提供一些有关此URL的背景信息?您是否要创建可共享的URL?为链接/操作组装自定义URL吗?Can you provide some context as to what you would want to do with this URL? Are you trying to create sharable URLs? Assemble custom URLs for links/actions?
- 0
- 2017-07-25
- Tom J Nowell
-
@TomJNowell我想加入一个特定的JS脚本,但仅在某些页面上入队(在这种情况下,这些页面是我网站以各种语言显示的主页:https://www.example.com/、https://www.example.com/fr/,https://www.example.com/es/等).JS文件将服务器添加到仅显示在首页上的多个标题的超链接.@TomJNowell I would like to enqueue a particular JS script, but only on certain pages (in this case, those pages are the homepage of my site in various languages: https://www.example.com/, https://www.example.com/fr/, https://www.example.com/es/, etc). The JS file will server to add hyperlinks to several titles that appear only on the homepage.
- 0
- 2017-07-25
- cag8f
-
为什么不只使用`is_home()`或`is_page('fr')`等,仅在脚本为真时才入队?why not just use `is_home()` or `is_page( 'fr' )` etc and only enqueue the script if it's true?
- 0
- 2017-07-25
- Tom J Nowell
-
@TomJNowell现在,我的代码是`if(home_url($ wp-> request)==home_url()){wp_enqueue_script();}`这似乎在每个主页上触发,无论使用哪种语言.那是你的建议吗?@TomJNowell Well now my code is `if ( home_url( $wp->request ) == home_url() ) { wp_enqueue_script();}` This appears to fire on every home page, regardless of language. Is that what you were suggesting?
- 0
- 2017-07-26
- cag8f
-
为什么不使用`$ _SERVER ['REQUEST_URI']`和公司呢?看到这个问题:https://stackoverflow.com/q/6768793/247696Why not use `$_SERVER['REQUEST_URI']` and company? See this question: https://stackoverflow.com/q/6768793/247696
- 1
- 2019-05-29
- Flimm
-
10 个回答
- 投票数
-
- 2017-07-25
get_permalink()
仅对单个页面和帖子真正有用,并且仅在循环内有效.我所见过的最简单的方法是:
global $wp; echo home_url( $wp->request )
$wp->request
包含URL的路径部分,例如./path/to/page
和home_url()
在"设置">"常规"中输出URL,但是您可以为其添加路径,因此我们将请求路径附加到此代码中的首页网址.请注意,这可能不适用于将永久链接设置为Plain的情况,并且会忽略查询字符串(URL的
?foo=bar
部分).要在永久链接设置为纯文本时获取URL,可以改为使用
$wp->query_vars
,将其传递给add_query_arg()
:global $wp; echo add_query_arg( $wp->query_vars, home_url() );
并且您可以结合使用这两种方法来获取当前URL,包括查询字符串,而与永久链接设置无关:
global $wp; echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );
get_permalink()
is only really useful for single pages and posts, and only works inside the loop.The simplest way I've seen is this:
global $wp; echo home_url( $wp->request )
$wp->request
includes the path part of the URL, eg./path/to/page
andhome_url()
outputs the URL in Settings > General, but you can append a path to it, so we're appending the request path to the home URL in this code.Note that this probably won't work with Permalinks set to Plain, and will leave off query strings (the
?foo=bar
part of the URL).To get the URL when permalinks are set to plain you can use
$wp->query_vars
instead, by passing it toadd_query_arg()
:global $wp; echo add_query_arg( $wp->query_vars, home_url() );
And you could combine these two methods to get the current URL, including the query string, regardless of permalink settings:
global $wp; echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );
-
如果永久链接设置为纯格式:`echo'//'.$ _SERVER ['HTTP_HOST'].$ _SERVER ['REQUEST_URI'];`.If permalinks set to plain: `echo '//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];`.
- 7
- 2017-07-25
- Max Yudin
-
@Jacob我尝试过,但似乎只返回我主页的URL.如您在本页左上方所见(https://dev.horizonhomes-samui.com/properties/hs0540/),我在其中插入了代码到`echo home_url($ wp-> request)`中.我确保也包括`global $ wp`.永久链接不是"普通",而是设置为"帖子名称".我也没有在日志中看到任何相关的PHP错误.此特定页面是我的开发站点的一部分,否则将其阻止访问者访问.我不确定这是否重要.编辑:实际上,请保持这种想法-可能是用户错误.支持...@Jacob I tried that, but it seems to be returning the URL of my homepage only. As you can see in the top left on this page (https://dev.horizonhomes-samui.com/properties/hs0540/), where I have inserted code to `echo home_url( $wp->request )`. I have ensured to include `global $wp` as well. Permalinks are not 'Plain,' but set to 'Post Name.' I don't see any relevant PHP errors in the log either. This particular page is part of my dev site, which is otherwise blocked off to visitors. I'm not sure if that matters or not. edit: Actually, hold that thought--could be user error. Stand by...
- 2
- 2017-07-25
- cag8f
-
@Jacob编辑2:确定您的代码确实起作用.我的问题是我将代码包含在functions.php'naked'中,即未包含在连接到钩子的任何函数中.因此,无论我的浏览器中显示的页面是什么,您的代码都将返回主页的URL.一旦将代码移到一个函数中(一个附加到WP钩子的函数(wp_enqueue_scripts)),它确实返回了所显示页面的URL.您知道这种行为的原因吗?也许我需要为此提出一个新问题.@Jacob edit 2: OK your code does indeed work. My issue was that I was including the code in functions.php 'naked,' i.e. not in any function that is attached to a hook. So your code was returning the URL of the homepage, regardless of the page that was displayed in my browser. Once I moved the code inside a function--a function attached to a WP hook (wp_enqueue_scripts), it did indeed return the URL of the displayed page. Do you know the reason for that behavior? Maybe I need to create a new question for that.
- 1
- 2017-07-25
- cag8f
-
@ cag8f如果代码在functions.php中处于"裸"状态,则您需要在$ wp对象的所有属性都已设置之前运行它.当将其包装到附加到适当的钩子的函数中时,您将延迟其执行,直到运行Wordpress代码中的适当点为止.@cag8f If the code sits "naked" in functions.php then you are running it before all the properties of the $wp object have been set up. When you wrap it in a function attached to an appropriate hook then you are delaying its execution until a suitable point in the Wordpress code run.
- 3
- 2018-04-05
- Andy Macaulay-Brook
-
这些方法都很棒,并且是使用WordPress的好主意.您可以根据自己的需要向其中添加"trailingslashit()".These methods are all awesome, and great ideas for working with WordPress. You might add `trailingslashit()` to them though, depending on your needs.
- 0
- 2019-10-17
- Jake
-
- 2018-04-05
您可以使用以下代码在WordPress中获取整个当前URL:
global $wp; $current_url = home_url(add_query_arg(array(), $wp->request));
这将显示完整路径,包括查询参数.
You may use the below code to get the whole current URL in WordPress:
global $wp; $current_url = home_url(add_query_arg(array(), $wp->request));
This will show the full path, including query parameters.
-
嗨-如果您看看https://developer.wordpress.org/reference/functions/add_query_arg/,您会发现您的代码实际上并未保留现有的查询参数.Hi - if you have a look at https://developer.wordpress.org/reference/functions/add_query_arg/ you'll see that your code doesn't actually preserve existing query parameters.
- 0
- 2018-04-05
- Andy Macaulay-Brook
-
为了保留查询参数,您需要用$ _GET替换空的array().即: `home_url(add_query_arg($ _ GET,$ wp-> request));`To preserve query parameters you'd need to replace the empty `array()` with `$_GET`. ie: `home_url(add_query_arg($_GET,$wp->request));`
- 5
- 2018-06-14
- Brad Adams
-
如果将WordPress安装在子目录中,则无法使用It won’t work if WordPress is installed in subdirectory
- 0
- 2018-11-26
- ManzoorWani
-
- 2019-10-21
为什么不只是使用?
get_permalink(get_the_ID());
Why not just use?
get_permalink(get_the_ID());
-
+1所有其他答案非常复杂,这只是最简单的解决方案+1 all other answers are much to complicated, this is just the simplest solution
- 2
- 2020-04-16
- Mark
-
这是最简单的方法.+1This is the easiest way. +1
- 1
- 2020-05-23
- Syafiq Freman
-
不适用于我网站上的博客类别doesn't work with blog categories on my site
- 0
- 2020-06-21
- Iggy
-
对于类别页面,使用`get_category_link(get_query_var('cat'));`For category pages, use `get_category_link( get_query_var( 'cat' ) );`
- 0
- 2020-06-23
- Dario Zadro
-
- 2018-12-28
以下代码将提供当前网址:
global $wp; echo home_url($wp->request)
您可以使用以下代码来获取完整的URL和查询参数.
global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request));
这将显示完整路径,包括查询参数.如果网址中已有查询参数,则会保留该参数.
The following code will give the current URL:
global $wp; echo home_url($wp->request)
You can use the below code to get the full URL along with query parameters.
global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request));
This will show the full path, including query parameters. This will preserve query parameters if already in the URL.
-
该代码段跳过了我当前URL中的`wp-admin/plugins.php`,它只是根路径和查询字符串.This snippet skips `wp-admin/plugins.php` in my current URL, it's only the root path and query strings.
- 0
- 2019-08-03
- Ryszard Jędraszyk
-
- 2018-11-29
function current_location() { if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $protocol = 'https://'; } else { $protocol = 'http://'; } return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } echo current_location();
function current_location() { if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $protocol = 'https://'; } else { $protocol = 'http://'; } return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } echo current_location();
-
您能解释一下该代码如何以及为什么解决了这个问题吗?Can you explain how and why this code solves the question?
- 0
- 2018-11-29
- kero
-
我认为最灵活的解决方案.它可以在任何WP页面上运行(甚至在wp-admin,wp-login.php,存档页面等上).请注意,它不包含任何URL参数In my opinion the most flexible solution. It works on any WP page (even on wp-admin, wp-login.php, archive pages, etc). Just notice, that it does not include any URL params
- 0
- 2019-04-01
- Philipp
-
- 2018-06-11
这是前面提到的改进的示例方式.当启用漂亮的URL时,它可以工作,但是如果有任何查询参数(例如/page-slug/?param=1 )或URL十分丑陋,它将丢弃.
以下示例在两种情况下均适用.
$query_args = array(); $query = wp_parse_url( $YOUR_URL ); $permalink = get_option( 'permalink_structure' ); if ( empty( $permalink ) ) { $query_args = $query['query']; } echo home_url( add_query_arg( $query_args , $wp->request ) )
This is an improved way of example that mentioned previously. It works when pretty URLs are enabled however it discards if there is any query parameter like /page-slug/?param=1 or URL is ugly at all.
Following example will work on both cases.
$query_args = array(); $query = wp_parse_url( $YOUR_URL ); $permalink = get_option( 'permalink_structure' ); if ( empty( $permalink ) ) { $query_args = $query['query']; } echo home_url( add_query_arg( $query_args , $wp->request ) )
-
- 2019-10-01
也许
wp_guess_url()
是你的意思需要.从2.6.0版开始可用.Maybe
wp_guess_url()
is what you need. Available since version 2.6.0.-
这只是猜测基本URL.在前端,您最终会获得与`home_url()`类似的效果.This just guesses the base URL. On the frontend, you end up with a similar effect to `home_url()`.
- 0
- 2019-10-17
- Jake
-
- 2020-04-04
在对一个简单任务进行了大量研究之后,以上所有答案的混合对我们而言很有效:
function get_wp_current_url(){ global $wp; if('' === get_option('permalink_structure')) return home_url(add_query_arg(array($_GET), $wp->request)); else return home_url(trailingslashit(add_query_arg(array($_GET), $wp->request))); }
末尾没有丢失斜杠,依此类推.作为有关输出当前URL的问题ID,这与安全性和内容无关.但是,在PHP中找不到像#comment这样的哈希.
After so much research of a simple task, a mix of all answers above works for us:
function get_wp_current_url(){ global $wp; if('' === get_option('permalink_structure')) return home_url(add_query_arg(array($_GET), $wp->request)); else return home_url(trailingslashit(add_query_arg(array($_GET), $wp->request))); }
No missing slash at the end and so on. As the question id about output the current url, this doesnt care about security and stuff. However, hash like #comment at the end cant be found in PHP.
-
- 2020-07-15
这是对我有用的(简短且干净的解决方案,其中也包含URL中的查询字符串):
$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
输出网址如下所示
http://sometesturl.test/slug1/slug2?queryParam1=testing&queryParam2=123
该解决方案来自此处
This is what worked for me (short and clean solution that includes the query strings in the URL too):
$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
The output URL will look like below
http://sometesturl.test/slug1/slug2?queryParam1=testing&queryParam2=123
The solution was taken from here
-
- 2020-07-28
我意识到这是一个老问题,但是我注意到的一件事是使用
get_queried_object()
时没有提及.这是一个全局wp函数,可捕获与您当前所在的URL相关的所有内容.因此,例如,如果您在页面或帖子上,它将返回一个post对象.如果您使用的是归档文件,它将返回一个帖子类型的对象.
WP还具有许多辅助功能,例如
get_post_type_archive_link
,您可以向对象提供posttype字段并像这样获取其链接get_post_type_archive_link(get_queried_object()->name);
重点是,您不必依赖上面的某些骇客回答,而可以使用查询的对象始终获取正确的网址.
这同样适用于无需额外工作的多站点安装,因为使用wp的功能,您始终可以获得正确的URL.
I realize this is an old question, however one thing I've noticed is no-one mentioned using
get_queried_object()
.It's a global wp function that grabs whatever relates to the current url you're on. So for instance if you're on a page or post, it'll return a post object. If you're on an archive it will return a post type object.
WP also has a bunch of helper functions, like
get_post_type_archive_link
that you can give the objects post type field to and get back its link like soget_post_type_archive_link(get_queried_object()->name);
The point is, you don't need to rely on some of the hackier answers above, and instead use the queried object to always get the correct url.
This will also work for multisite installs with no extra work, as by using wp's functions, you're always getting the correct url.
我想添加自定义PHP代码,以确保每当我浏览器中加载网站上的页面时,该页面的URL都会回显到屏幕上.我可以使用
echo get_permalink()
,但这不适用于所有页面.一些页面(例如我的主页)显示了几篇文章,如果我使用get_permalink()
在这些页面上,不会返回所显示页面的URL(我相信它会返回循环中最后一个帖子的URL).对于这些页面,如何返回URL?我可以将
get_permalink()
附加到在执行循环之前触发的特定挂钩吗?还是我可以以某种方式跳出循环,或者在完成后将其重置?谢谢.