2017-04-17 69 views
-1

我想以缩略图的形式显示用户的收藏帖子。 目前使用此代码:获取用户最喜欢的帖子缩略图

<?php 
    $post_id = array(get_user_favorites($user_id, $site_id)); 
    $loop = new WP_Query($post_id); 
    if($loop->have_posts()): 
     while ($loop->have_posts()): 
      $loop->the_post(); 
      if (has_post_thumbnail()) : // thumbnail check 
       $image = wp_get_attachment_image_src(get_post_thumbnail_id());         

?> 
      <div> 
       <img src="<?php echo esc_url($image[0]); ?>" /> 
      </div> 
<?php 

      endif; 
     endwhile; 
    endif; 
    wp_reset_query(); 
?> 

这显示所有我的博客文章作为缩略图而不是收藏了职。 有什么帮助吗?

回答

0

get_user_favorites()https://favoriteposts.com/)将返回一个post ID的数组。

您正在将这些帖子ID封装在一个数组中,并试图将它们传递给WP_Query。你需要指定你试图设置哪个参数,这些参数在本例中是'post_in'。

/** 
* I'm assuming based on the code in your question that you 
* have access to $user_id and $site_id already. 
*/ 
$post_ids = get_user_favorites($user_id, $site_id); 

/** 
* We only want to attempt to display the favorite post images 
* IF favorites are set. 
*/ 
if ($post_ids) { 
    $loop = new WP_Query(array(
     'posts_per_page'  => -1, // display all favorited posts. Perhaps limit? 
     'ignore_sticky_posts' => true, 
     'post__in'   => $post_ids 
    )); 

    // Output here... 
} 

我引用无论是在插件的主页文档和他们的代码样本中提供的要点是:https://gist.github.com/kylephillips/9fe12f195c671c989af3