【问题标题】:how to hide featured image from SOME posts如何从一些帖子中隐藏特色图片
【发布时间】:2012-11-28 02:05:10
【问题描述】:

有没有办法从某些帖子中隐藏特色图片?

我的博客是cur-mudg-eon.com,如果您查看最近的帖子(在主页上),标题为“孔子说...”,您会发现我使用了特色图片并显示了一些摘录文本。当您单击标题或图片时,它会将您带到显示我想要显示的卡通以及我想要隐藏/删除的特色图像的帖子。

我只想在某些帖子上这样做,但我希望能够将特色图片保留在主页上。

这可能吗?

编辑:

Pastebin File 应要求。

根据 Chris Herberts 的回答,我会将他的代码添加到我的 single.php 文件中的代码中:

    <?php if(has_post_thumbnail()) {
        echo '<figure class="featured-thumbnail"><span class="img-wrap">'; the_post_thumbnail(); echo '</span></figure>';
        }
      ?>
    <?php } else { ?>
      <?php if(has_post_thumbnail()) {
        echo '<figure class="featured-thumbnail large"><span class="img-wrap"><span class="f-thumb-wrap">'; the_post_thumbnail('post-thumbnail-xl'); echo '</span></span></figure>';
        }
      ?>
    <?php } ?> 

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    另一种不依赖于它们都属于同一类别的方法是使用Custom Field

    您将为帖子设置一个自定义字段,您将为其隐藏特色图片 - 在下图中,我分别使用“hide_featured_image”和“yes”作为键和值。

    然后,您将在调用函数以显示特色图像时检查“hide_featured_image”字段是否设置为“yes”。这是一个例子:

    $shouldHideFeaturedImage = get_post_meta($post->ID, 'hide_featured_image', true);
    
    if ( $shouldHideFeaturedImage != 'yes' ) {
     if ( has_post_thumbnail() ) {
        the_post_thumbnail('medium'); 
        } 
    }
    

    【讨论】:

    • 对于 WP 新手(比如我自己),您需要“取消隐藏”自定义字段。为此,您需要点击“帖子”,然后点击相关帖子的“编辑”。进入帖子的编辑屏幕后,单击右上角的“屏幕选项”选项卡,并确保“自定义字段”框中有复选标记。
    • 谢谢克里斯。如果您能帮我将您的代码添加到我刚刚添加到原始帖子中的代码中,我们将不胜感激。
    • 你能多贴一些页面的代码吗?您发布的 sn-p 缺少结束 ?> 标记,我不想搞砸任何事情。
    • 其实把整个页面贴到pastebin什么的。您的 sn-p 中有括号不匹配。
    • 以前从未见过 pastebin.... 我喜欢它。 Pastebin link
    【解决方案2】:

    如果所有这些帖子都属于同一类别,您可以这样做。

    在你的主题文件中,在文件 single.php 下应该有类似这样的内容:

    if ( has_post_thumbnail() ) {
    the_post_thumbnail('medium'); 
    } 
    

    将其更改为:

    if ( !in_category( array( 'category1', 'category2', 'etc' ) )) {
     if ( has_post_thumbnail() ) {
        the_post_thumbnail('medium'); 
        } 
    }
    

    【讨论】:

    • 这看起来效果很好,但帖子可能并不总是属于同一类别。万一其他人正在查看此选项,是否可以为其他类别添加额外的“if”语句?
    • 当然。我编辑了响应,因此可以添加一个包含类别名称的数组。
    猜你喜欢
    • 2020-04-07
    • 2016-02-28
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2012-06-30
    • 2013-11-08
    相关资源
    最近更新 更多