获取用户喜爱的帖子缩略图

时间:2017-04-17 18:17:46

标签: php wordpress

我正在尝试将用户的收藏帖子显示为缩略图。 目前使用此代码:

<?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();
?>

这显示所有我的博客文章作为缩略图但不是有利的帖子。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

get_user_favorites()https://favoriteposts.com/)将返回一系列帖子ID。

您将这些帖子ID包装在一个数组中并尝试将它们传递给WP_Query。您需要指定您尝试使用这些ID设置的参数,在这种情况下,这些ID将是“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

相关问题