函数文档

the_ID()

💡 云策文档标注

概述

the_ID() 是 WordPress 核心函数,用于在 WordPress Loop 中直接输出当前项目的 ID。它基于 get_the_ID() 实现,主要用于模板开发。

关键要点

  • the_ID() 直接输出当前项目的 ID,通常用于在 Loop 中显示文章、页面等内容的唯一标识符。
  • 函数内部调用 get_the_ID() 来获取 ID,因此功能上等同于 get_the_ID() 但直接输出而非返回值。
  • 自 WordPress 0.71 版本引入,是 post-template.php 文件中的核心函数。
  • 常用于模板文件如 single.php 或 archive.php 中,以提供唯一锚点或标识。

代码示例

// 在 Loop 中使用 the_ID() 输出当前文章 ID
while ( have_posts() ) : the_post();
    the_ID(); // 直接输出 ID
endwhile;

注意事项

  • the_ID() 仅适用于 WordPress Loop 内部,在 Loop 外部调用可能不会返回预期结果。
  • 与 get_the_ID() 不同,the_ID() 直接输出 ID,不能用于赋值或进一步处理,需根据场景选择使用。
  • 函数名遵循 WordPress 命名规范,但 PHPCS 可能标记为无效,可忽略此警告。

📄 原文内容

Displays the ID of the current item in the WordPress Loop.

Source

function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	echo get_the_ID();
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes