English 中文(简体)
获取当前帖子的WordPress帖子ID
原标题:Getting the WordPress Post ID of current post

有人知道我如何获得当前页面的帖子ID吗?

因此,如果我在header.php中的某个特定帖子上,我希望能够获得当前的帖子id。

谢谢

最佳回答

尝试使用此:

$id = get_the_ID();
问题回答

您可以使用$post->;ID以获取当前ID。

在大多数情况下,get_the_ID()将正常工作:

$post_id = get_the_ID();

但是,在某些情况下,您可能需要使用get_queried_object_id()改为:

$post_id = get_queried_object_id();

原因是get_The_ID()返回的值依赖于上下文,在某些情况下,可能会返回与所查询的主帖子不同的ID。有关更多信息,我建议阅读WordPress关于循环

global $post;
echo $post->ID;

尝试:

$post = $wp_query->post;

然后传递函数:

$post->ID

您可以通过以下代码获取id。。。它简单快捷

 <?php $post_id = get_the_ID();
   echo $post_id;
   ?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签