$ post-> ID显示错误的ID
-
-
您是否只是在尝试获取该特定ID,还是要使用某些_generalfunctional_处理这些ID?如果是第一种情况,则可以使用`get_option('page_for_posts');`.Are you just trying to get that specific ID, or do you want to have some _general functionality_ dealing with the IDs? If the first is the case, you could use `get_option('page_for_posts');`.
- 1
- 2013-08-08
- tfrommen
-
我试图获取metabox的价值,因此无论如何现在都可以解决ID的常规功能,谢谢您的宝贵时间!I was trying to get metabox value, so general functionality dealing with the IDs, anyways it's resolved now, thanks for your time!
- 0
- 2013-08-08
- Wordpressor
-
这是一个类似的线程:https://wordpress.stackexchange.com/questions/150192/at-my-posts-archive-page-outside-the-loop-get-the-id-returns-the-top-most-poHere's a similar thread: https://wordpress.stackexchange.com/questions/150192/at-my-posts-archive-page-outside-the-loop-get-the-id-returns-the-top-most-po
- 0
- 2019-03-05
- Pikamander2
-
1 个回答
- 投票数
-
- 2013-08-08
在该页面上,$post-> ID返回给定的第一篇博客文章的ID 页面.
这就是它的工作方式.
$post
设置为循环中的第一条帖子.在与帖子或页面相同的单个帖子和页面上.在存档页面上,它是结果集中的第一条帖子.如果考虑到这一点,两者实际上是同一回事.单个帖子和页面在结果集中只有一个结果,恰好与您期望的帖子或页面相匹配.现在,棘手的部分,我试图从我所有的东西中删除所有循环 页面,完全擦除了loop.php文件,禁用了所有 可能已经影响了$post,但它仍然返回错误的ID.
主查询在模板加载之前运行,并且在该过程中设置了
$post
.从模板中删除内容不会对此进行更改.有任何提示吗?
是的.除了在适当的循环内之外,不要依赖
$post
.如果您需要有关页面本身的信息,请使用:$pobj = get_queried_object(); var_dump($pobj); // debugging
参考:
http://codex.wordpress.org/Function_Reference/get_queried_object
On that page $post->ID returns the ID of first blog post for given page.
That is how it works.
$post
is set to the first post in the Loop. On single posts and pages that is the same as the post or page. On archive pages it is the first post in the result set. And if you think about that, both are really the same thing. Single posts and pages only have one result in the set which happens to match the post or page that you expect.Now, the tricky part, I've tried to remove all the loops from all my pages, totally erased loop.php file, disabled all the widgets that might have been affecting $post and it still returns the wrong ID.
The main query runs before your template loads and
$post
is set in that process. Removing things from the template won't change that.Any hints?
Yes. Don't rely on
$post
except inside a proper Loop. If you need information about the page itself use:$pobj = get_queried_object(); var_dump($pobj); // debugging
Reference:
http://codex.wordpress.org/Function_Reference/get_queried_object
-
聪明,我以前从未听说过这种解决方案.Clever, I have never heard about this solution before.
- 0
- 2013-08-08
- Wordpressor
这很奇怪.
此代码:
除博客页面(/wp-admin/options-reading.php上设置为博客的页面)外,每次都显示权利ID.
在该页面上,$post-> ID返回给定页面的第一篇博客文章的ID.
现在,棘手的部分是,我尝试删除所有页面上的所有循环,完全擦除了loop.php文件,禁用了可能已经影响$post的所有小部件,并且它仍然返回错误的ID.
有任何提示吗?
我的index.php非常标准:
loop.php是:
也许是我要在header.php中获取ID?
是的,
wp_reset_postdata()
似乎也没有帮助:/