当用户访问WordPress中的帖子页面时,如何获取帖子信息


How to get post info, when user visits a post page in WordPress?

当用户访问特定的帖子或页面(主页除外)时,如何在php中获取帖子信息(帖子的至少ID)?哪个动作或过滤器最适合这个?

我想检测用户何时访问页面或帖子,并制作一个像这样的html日志:

a user visited the page <a href="link_to_post"> Post Title </a>

我目前有the_post操作,但它也在主页中运行。

您可以使用is_home()来检查它是否是主页。您可以使用get_permalinkget_the_title获取链接并发布标题

试试这个:

if (!is_home()) {
    echo 'a user visited the page <a href="' . get_permalink() . '">' . get_the_title() . '</a>';
}