我无法在循环之外获取帖子作者ID,以使get_the_author_meta正常工作。到目前为止,我尝试了不同的方法:

1.

$author_id=$post->post_author;


2。 >
3.

global $post;
$author_id=$post->post_author;


4.

$post_tmp = get_post($post_id);
$author_id = $post_tmp->post_author;


我需要作者ID才能通过放在以下位置:

$author_id = $posts[0]->post_author;


有任何建议吗?

评论

检查一下,这对我有用。

#1 楼

如果您知道帖子ID,则在循环外获取帖子作者ID的最简单,最直接的方法是使用WordPress核心功能get_post_field()

$post_author_id = get_post_field( 'post_author', $post_id );


如果您尚不知道所在页面的帖子ID,那么自WP 3.1以来,最简单的方法是使用get_queried_object_id()(在方法列表中查找)功能,该功能甚至可以在循环外使用。

$post_id = get_queried_object_id();


如果这些对您不起作用,请提供更详细的解释,说明您尝试在何处运行代码,我们可以看看是否可以进一步提供帮助。

#2 楼

以下是在WordPress循环之外获取和获取作者ID的方法:

<?php
global $post;
$author_id=$post->post_author;
?>


那么我们可以the_author_meta: />

评论


如果您有权访问帖子ID,则效果很好。如果您不想立即输出值,也可以使用get_the_author_meta('user_nicename',$ author_id)

– Andrew M
16年12月21日在9:44

#3 楼

取决于你在哪里。如果您使用的页面是单个页面(例如,仅显示单个{{在此处插入帖子类型}}),则可以使用get_queried_object,它将获取帖子对象。
如果您在其他任何地方,都可以使用全局$wp_query对象,并检查其$posts属性。这也应该在单个页面上起作用。这将不会引起任何其他数据库命中或类似情况。 WordPress一次获取所有帖子(在撰写本文时)。 rewind_posts只是将当前发布(全局$post)对象重置为数组的开头。不利的一面是,这可能会导致loop_start操作比您希望的更早触发-没什么大不了的,只是需要注意的事情。

#4 楼

看来它可以在循环外运行,也许会有所帮助。

    $thelogin = get_query_var('author_name');
    $theauthor = get_userdatabylogin($thelogin);


您还可以手动设置帖子ID并以此方式获取: br />
global $wp_query;
$thePostID = $wp_query->post->ID;
$postdata = get_post($thePostID, ARRAY_A);
$authorID = $postdata['post_author'];


将ID更改为手动发布ID,以进行循环访问。

#5 楼

在尝试制作一个显示带有作者信息的特色帖子的小部件时,我遇到了同样的问题。

我使用了@chrisguitarguy第二个技巧中的一些提示。看起来像这样:

<?php    

$count = 0;
$query_args = array(
      'posts_per_page' => 5,
     );
$com_query = new WP_Query( $query_args );

$feat_posts = $com_query->posts; // array, so we can access each post based on position

while ($com_query->have_posts()) {              
    $com_query->the_post();
        $author_name= get_the_author_meta('user_nicename',  $feat_posts[$count]->post_author);
        $count++;
}


#6 楼

要获取并获得作者ID,请使用以下代码:

global $post;
$author_id = $post->post_author;


然后使用

get_the_author_meta('field_name', $author_id)


请记住正在以循环方式获取帖子ID并以外部循环访问作者,那么它将仅提供循环中最后一个帖子ID的数据

#7 楼

希望这会有所帮助:

$args= array(
    'post_type' =>'any',
    'post_status' => 'publish',
    'order' => 'ASC',
    'posts_per_page' => '-1'
);
$posts = new WP_Query($args);
$posts = $posts->posts;   

foreach($posts as $post) { 
  switch ($post->post_type) {
     case 'page': 
           // get the author's id through the post or page
           $id = get_post_field( 'post_author', $post->ID);
           // the first parameter is the name of the author 
           // of the post or page and the second parameter 
           // is the id with which the function obtains the name of the author.
           echo get_the_author_meta('display_name', $id);
        break;
    case 'post': 
         $id = get_post_field( 'post_author', $post->ID;
        echo get_the_author_meta('display_name', $id);
  }
}


#8 楼

为什么不使用the_author_meta

<p>The email address for user id 25 is <?php the_author_meta('user_email',25); ?></p>


可以在循环中使用

评论


谢谢,但是问题是我不在循环中,无法解决该问题。当您不在循环中时,需要提供第二个参数($ author_id)。

– Marce Castro
2012-09-18 22:16

磕碰!有任何想法吗?这让我疯狂 :-/

– Marce Castro
2012-09-21 18:41

在循环之外-请注意问题。

–克里斯汀·库珀♦
2012年12月26日在18:28