网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   WordPress (https://www.0058.net/forumdisplay.php?f=68)
-   -   wordpress 通过get_the_ID和the_ID函数获取文章ID (https://www.0058.net/showthread.php?t=5173)

Jamison 2015-06-14 06:18 AM

wordpress 通过get_the_ID和the_ID函数获取文章ID
 
get_the_ID() 返回当前文章的ID号,此标签必须在主循环里。

用法:<?php get_the_ID(); ?>

示例:

eg1:

<?php

$id = get_the_ID();

$dropdown = "<select name='dropdown-".$id."' >";

$dropdown.="<option id='option1-".$id."'>Option 1</option>";

$dropdown .= "</select>";

echo $dropdown;

?>

eg2:

<?php

echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'" />';

?>

the_ID() 输出当前文章的ID号,此标签必须在主循环里。

用法:<?php the_ID(); ?>

示例:

eg1:
<p>Post Number:<?php the_ID(); ?></p>

eg2:
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>

源文件:get_the_ID()和the_ID() 都在 wp-includes/post-template.php文件.


All times are GMT +8. The time now is 12:25 AM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.