返回介绍

get_the_ID()

发布于 2017-09-11 00:35:16 字数 3661 浏览 1154 评论 0 收藏 0

get_the_ID()

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


description


返回值

(int|false) The ID of the current item in the WordPress Loop. False if $post is not set.


源代码

File: wp-includes/post-template.php

function get_the_ID() {
	$post = get_post();
	return ! empty( $post ) ? $post->ID : false;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_post()

Used By

  • wp-admin/includes/dashboard.php: wp_dashboard_recent_posts()
  • wp-includes/link-template.php: get_post_comments_feed_link()
  • wp-includes/post-template.php: prepend_attachment()
  • wp-includes/post-template.php: the_ID()
  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/media.php: wp_audio_shortcode()
  • wp-includes/post.php: is_sticky()
  • wp-includes/post.php: get_post_custom()
  • wp-includes/comment-template.php: get_comment_id_fields()
  • wp-includes/comment-template.php: wp_list_comments()
  • wp-includes/comment-template.php: comment_form()
  • wp-includes/comment-template.php: get_trackback_url()
  • wp-includes/comment-template.php: comments_popup_link()
  • Show 8 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex

    Post Anchor Identifier
    get_the_ID() can be used to provide a unique anchor in a script. For instance, a dynamically-generated drop down menu with actions for each post in an archive could have

    
    <?php
    	$id = get_the_ID();
    	$dropdown = "<select name='dropdown-".$id."' >";
    	$dropdown .= "<option id='option1-". $id ."'>Option 1</option>";
    	$dropdown .= "</select>";
    ?>
    

    This would allow us to use JavaScript to control the element as it has a unique ID, and when submitting it as a form through the POST or GET methods the dropdown box will be sent with a unique ID which allows the script to note which post it is working on. Alternatively a hidden variable could be sent which will allow the script to see which post the submission is referring to

    
    <?php
    	echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'" />';
    ?>
    
  2. Store the ID
    The ID can be stored as a variable using

    
    <?php $postid = get_the_ID(); ?>
    

    Should be noted that if this is ran on the Blog homepage, it instead returns the first listed Post ID instead of the blog homepage ID.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文