如何在循环外获取当前页面ID?
-
-
奇怪,您如何使用代码,尝试了什么.您遇到什么错误.就目前情况而言,很难为您提供帮助,所有答案都是黑暗中的一击.请提交[编辑]Strange, how are you using the code, what have you tried. What errors are you getting. As it stands, it is difficult to help you and all answers is a shot in the dark. Please file an [edit]
- 3
- 2014-09-17
- Pieter Goosen
-
` ID;?> ``ID;?> `
- 0
- 2014-09-17
- Muhammad Shoaib Murtaza
-
请提交[edit]及其所有附加代码/信息.不要在问题中使用评论部分:-)Please file an [edit] with all the extra code/info. Don't use the comment section for stuff that should be in your question :-)
- 3
- 2014-09-17
- Pieter Goosen
-
您的帖子已被自动标记为低质量,请遵守我的上述要求:-)Your post has been automatically flagged as low quality, please comply to my requests above :-)
- 0
- 2014-09-17
- Pieter Goosen
-
我没有任何错误,实际上我想在我的页面上显示googlemap,因此地图不显示...i m not getting any errors, actually i want to display google map on my page, so map is't displaying...
- 0
- 2014-09-17
- Muhammad Shoaib Murtaza
-
在header.php中,我正在使用其他方式根据页面ID进行显示in header.php i am using if else to showing according to page ids
- 0
- 2014-09-17
- Muhammad Shoaib Murtaza
-
正如我所说,所有这些信息都应放在您的问题中,而不是在评论部分中.请[编辑]您的问题As I said, all of that info should go into your question, not in the comments section. Please [edit] your question
- 1
- 2014-09-17
- Pieter Goosen
-
亲爱的@Programmer,请阅读[问].您收到关于帖子质量的自动标记.您得到了如何解决该问题的建议.请尝试解决该问题,以便您的问题保持开放状态,并且您不会收到反对票.谢谢.Dear @Programmer, please read [ask]. You received automatic flags about the quality of your post. You got suggestions how to fix that. Please try to get over that so your question can stay open and you don't receive down votes. Thank you.
- 0
- 2015-01-12
- kaiser
-
您已经接受了一个答案,但是正确的答案(可以说是一个重复的问题)在这里:http://wordpress.stackexchange.com/a/109468/21376You've accepted an answer already but the correct answer (arguably a duplicate question) is here: http://wordpress.stackexchange.com/a/109468/21376
- 0
- 2015-08-19
- s_ha_dum
-
3 个回答
- 投票数
-
-
我已经尝试了所有在Google上找到的东西,但没有任何反应i have tried all the stuff found on google but nothing happened
- 0
- 2014-09-17
- Muhammad Shoaib Murtaza
-
很难知道什么是"找到的所有东西" :)确保代码在函数中,您可以调用该函数并通过回显之前必须可见的其他内容开始调试...it's hard to know what "all the stuff you found" is :) make sure the code is in a function, that you call that function and start debuging with echoing something else before which has to be visible...
- 0
- 2014-09-17
- caramba
-
我没有使用功能获取ID,"东西"是 `global $ wp_query; $thePostID=$ wp_query->post-> ID; ` `global $post; $thePostID=$post-> ID; ` `global $ wp_query; $page=$ wp_query->get_queried_object(); $page_id=$page-> ID; ` `$page_id=get_queried_object_id(); `i did't getting id using function, and the "stuff" is `global $wp_query; $thePostID = $wp_query->post->ID; ` `global $post; $thePostID = $post->ID; ` `global $wp_query; $page = $wp_query->get_queried_object(); $page_id = $page->ID; ` `$page_id = get_queried_object_id(); `
- 2
- 2014-09-17
- Muhammad Shoaib Murtaza
-
第一个是正确的...first one was correct...
- 0
- 2014-09-17
- Muhammad Shoaib Murtaza
-
只有在循环开始之前使用这些解决方案,它们才会为您提供页面ID.一旦循环开始,`$post`将依次设置到循环中的每个帖子.These solutions will give you page ID only if used before the Loop starts. Once the Loop starts, `$post` will be set to each post in the Loop in turn.
- 2
- 2015-08-19
- s_ha_dum
-
-
- 2018-05-07
您可以轻松完成
$page_id = get_queried_object_id();
You can simply do,
$page_id = get_queried_object_id();
-
这应该被标记为答案...This should be marked as the answer...
- 5
- 2020-01-28
- Lezz
-
同意这应该是答案.Agreed. This should be the answer.
- 2
- 2020-03-08
- CJWEB
-
绝对应该是正确的答案.我正在尝试获取帖子或页面的当前ID,这是它为我提供正确ID而不用硬编码条件的唯一方法.Definately it should be the right answer. I'm trying to get the current id of a post or a page, and this is the only way it gives me the right Id without having to hardcode conditions.
- 0
- 2020-05-22
- Felipe Rugai
-
-
get_the_ID()是一个循环函数-又名,它打算在循环内部使用.因此,它并没有真正解决"如何在循环之外获取当前页面ID?"的问题.`get_the_ID()` is a Loop function-- aka, it is meant to be used inside the Loop. Therefore, it doesn't really address the question of "How to get current page id outside the loop?"
- 10
- 2015-08-19
- s_ha_dum
-
如何在循环之外获取当前页面ID?