有没有办法向未登录我的Wordpress博客的用户显示备用的特色图片?
这样我每个帖子就可以上传2张精选图片。 1表示已登录的用户,1表示未登录的访问者。
让我知道你的想法。感谢
答案 0 :(得分:1)
AFAIK每个帖子不能有多个精选图片。您可以使用我最喜欢的插件,附件之一来获得相同的功能,这些插件可以将媒体库项目附加到帖子中,以便使用php在模板文件中进行访问。
附件:http://wordpress.org/plugins/attachments/
因此,对于每个帖子,你可以给它一个特色图像,然后给它一个附加的图像。安装附件后,您可以选择将项目附加到标准后期编辑屏幕上的帖子中。一旦你这样做,这样的代码应该做你想要的:
<?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');
}
}
?>