【问题标题】:I need to get the post id for the first post of the current user in wordpress我需要在 wordpress 中获取当前用户的第一个帖子的帖子 ID
【发布时间】:2011-01-11 04:22:13
【问题描述】:

目前为止有这个

get_currentuserinfo(); $the_post = get_posts("author=" . $current_user->ID . "&posts_per_page=1"); $the_post = $the_post[0];

但我不确定如何从数组中获取 ID

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    get_posts() 将返回按发布日期降序排列的帖子数组。因此,使用您发布的代码,数组 ([0]) 中的第一篇文章将是作者发布的最后一篇文章。如果你真的想要用户的第一篇文章,你可以将 order 参数添加到调用 (&order=ASC) 以覆盖默认值。

    我认为 ID 的密钥只是“ID”,因此您可以使用 $the_post = $post[0]['ID'] 检索它。但我不得不承认我记不清了,文档也没有详细说明,它也可能是“post_id”。您可以执行以下操作:print_r($the_post) 来检查返回数组的键。

    【讨论】:

    • Wordpress Dokumenation@codex.wordpress.org/Function_Reference/get_posts 声明:要访问帖子的 ID 或内容而不调用 setup_postdata(),或者实际上任何帖子特定的数据(保留在帖子表中的数据),您可以使用 $ post->COLUMN,其中 COLUMN 是数据的表列名。所以 $post->ID 保存 ID,$post->post_content 保存内容,以此类推。要在页面上显示或打印此数据,请使用 PHP echo 命令,如下所示: ID; ?>
    • @Cadoc:我错了,你是对的,它返回一个帖子对象数组,所以 ID 将通过 $the_post[0]->ID 检索。
    【解决方案2】:

    你可以这样写查询-

     $oldest_post_query = get_posts("post_type=post&numberposts=1&order=ASC");
     $oldest_post_id = $oldest_post_query[0]->ID;
    
     //print the post title
     <h1><a href="'.get_permalink( $oldest_post_id ).'" rel="next">'.get_the_title( $oldest_post_id ).'</a></h1>';
    

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      相关资源
      最近更新 更多