1

有没有办法向未登录我的 Wordpress 博客的用户显示备用特色图片?

这样我就可以为每个帖子上传 2 张精选图片。1 表示已登录的用户,1 表示未登录的访问者。

让我知道你的想法。谢谢

4

1 回答 1

1

AFAIK you cannot have more than one featured image per post. You can get an equivalent functionality using one of my favorite plugins, attachments, which lets you attach media library items to a post, to be accessed in your template files with php.

Attachments: http://wordpress.org/plugins/attachments/

So for each post, you could give it a featured image, then also give it an attached image. Once you install attachments, you will have the option to attach items to a post on the standard post editing screen. Once you do that, some code like this should do what you want:

<?php

if (is_user_logged_in()) {
    //show featured image to logged in user
    the_post_thumbnail('thumbnail');
} else {
    //show alternate image to non logged in user, using the attachments plug in
    $attachments = new Attachments('attachments');
    //you know they will exist, but we do this check to be safe so we don't try to call methods from a null object
    if ($attachments->exist()) 
    {
        $attachments->get();
    //we could also loop like
    //while ($attachments->get()) {...}
    //if there were more than one, but for this purpose its just one, so then
        echo $attachments->image('thumbnail');
    }
}
?> 
于 2013-08-21T04:50:44.930 回答