我无法在Wordpress中获得post id

时间:2013-06-13 16:36:19

标签: wordpress

我使用此代码在wordpress中获取Post的id,但它返回空。这有什么不对?

 $post_id=get_the_ID();
 echo 'Post ID: ' . $post_id. "\n";

2 个答案:

答案 0 :(得分:0)

正如$post_id的Wordpress documentation所说:此标记必须位于循环内。

<?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            // your code here
        } // end while
    } // end if
?>

更多相关信息:http://www.thewordpressblog.com/basics/get-post-id-inside-and-outside-the-loop/

答案 1 :(得分:0)

好的..所以我这么写:

query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
    $pid=get_the_ID();
    echo "Post ID: ".$pid;
    endwhile;

// Reset Query
wp_reset_query();

获取页面中所有帖子的ID ..但我只需要一个帖子的ID。