^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_the_ID()用法示例

get_the_ID()

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


描述


返回值

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

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


源代码

File: wp-includes/post-template.php

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

更新日志

Version 描述
2.1.0 Introduced.

相关函数

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


  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.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈