【问题标题】:Different Featured Images for Non-Users on Wordpress?WordPress 上非用户的不同特色图片?
【发布时间】:2013-08-21 12:50:44
【问题描述】:

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

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

让我知道你的想法。谢谢

【问题讨论】:

    标签: wordpress


    【解决方案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');
        }
    }
    ?> 
    

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 2014-11-22
      • 2012-12-12
      • 1970-01-01
      • 2019-07-31
      • 2014-10-22
      • 1970-01-01
      • 2012-01-20
      • 2016-08-14
      相关资源
      最近更新 更多