A-A+
wordpress 通过get_the_ID和the_ID函数获取文章ID
在使用 wordpress 做项目时,遇到了在文章内容页里面要获取文章ID的函数,wordpress 内置了 get_the_ID 和 the_ID 函数,其中 get_the_ID() 是直接返回了,这个函数必须要在主循环内才可以使用,而 the_ID 函数则是直接输出了文章 ID,相当于 php 中 echo 文章id,直接把 id 给输出来了。下面来看一下其具体的用法。
用法:
示例1如下:
- <?php
- $id = get_the_ID();
- $dropdown = "<select name='dropdown-".$id."' >";
- $dropdown.="<option id='option1-".$id."'>Option 1</option>";
- $dropdown .= "</select>";
- echo $dropdown;
- ?>
示例2如下:
- <?php
- echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'" />';
- ?>
the_ID() 输出当前文章的ID号,此标签必须在主循环里。
实例用法:
- <?php the_ID(); ?>
示例1代码如下:
- Post Number:<?php the_ID(); ?>
示例2代码如下:
- <h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
源文件:get_the_ID()和the_ID() 都在 wp-includes/post-template.php文件.
评论已关闭!