【问题标题】:How to hide featured image in Wordpress Single Post如何在 Wordpress Single Post 中隐藏特色图片
【发布时间】:2015-09-01 07:16:24
【问题描述】:

我想在帖子页面中隐藏特色图片,但不是在主页上。
我查看了有关此问题的其他帖子,但它们对我不起作用,因此我将感谢您的帮助。

这是我的single.php

<div id="primary" class="full-width-page">
    <main id="main" class="site-main" role="main">

    <?php while ( have_posts() ) : the_post(); ?>

        <?php get_template_part( 'content', 'single' ); ?>

        <?php tesseract_post_nav(); ?>

        <?php
            // If comments are open or we have at least one comment, load up the comment template
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;
        ?>

    <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
</div><!-- #primary -->

【问题讨论】:

  • 打开'content-single.php'并编辑
  • 在 'content-single.php' 中,代码中应该有 get_the_post_thumbnail( $post_id, $size, $attr ) 注释。并检查

标签: wordpress wordpress-theming


【解决方案1】:

请写下您使用的主题。 根据您所写的内容,您必须编辑 content-single.php。 搜索这样的行:

get_the_post_thumbnail( $post->ID, ... );

the_post_thumbnail();

并删除或评论它。

【讨论】:

    【解决方案2】:

    将此添加到您的functions.php(首选子主题)中,就是这样。它适用于任何主题,您无需接触丑陋的 HTML 模板。

    function wordpress_hide_feature_image( $html, $post_id, $post_image_id ) {
      return is_single() ? '' : $html;
    }
    // add the filter
    add_filter( 'post_thumbnail_html', 'wordpress_hide_feature_image', 10, 3);
    

    参考资料: https://helloacm.com/how-to-hide-feature-image-of-posts-in-wordpress/

    【讨论】:

    • 我想抑制所有帖子、页面等渲染中的特色图像缩略图(我只通过一个联合图像的插件使用它们),我不知道这是否是最好的方法(或一种低效甚至错误的方法)来完成它,但我通过将 return is_single() 更改为 return has_post_thumbnail() 来做到这一点。
    【解决方案3】:

    代码在模板部分。您将在名为“content-single”的文件中找到特征图像功能。

    有两种方法可以禁用:

    1.找到代码。

    删除或注释您的 content-single 模板文件中的函数:

    <div class="thumbnail">
      <?php
        // Comment out this function:
        echo get_the_post_thumbnail( $post_id, $size, $attr );
        // or:
        the_post_thumbnail();
        // Or you can remove this <div> entirely
      ?> 
    </div>
    

    2。 CSS 方法。

    找到图像 div 的适当类并在样式表中添加不显示条件。

    .thumbnail{display:none}
    

    如果您可以分享网站网址,我可以更清楚地回答。

    【讨论】:

    • &lt;?php get_template_part( 'content', 'single' ); ?&gt; 但是你必须找到模板部分单独
    【解决方案4】:

    如何从 Tesseract Theme 单个帖子中删除特色图片背景:

    1.-首先从原始content-single.php文件中复制一份。

    2.- 使用纯文本编辑器编辑 content-single.php

    3.-如果原文件是这样的:

    <?php
    /**
    * @package Tesseract
    */
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
      <?php if ( has_post_thumbnail() && 'post' == get_post_type() ) {
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'tesseract-large' ); ?>
        <div class="entry-background" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>)">
            <header class="entry-header">
                <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
            </header><!-- .entry-header -->
        </div><!-- .entry-background -->
    
      <?php } else { ?>
        <header class="entry-header">
            <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
        </header><!-- .entry-header -->
      <?php } ?>
    
      <div class="entry-content">
        <div class="entry-meta">
            <?php tesseract_posted_on(); ?>
        </div><!-- .entry-meta -->
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'tesseract' ),
                'after'  => '</div>',
            ) );
        ?>
      </div><!-- .entry-content -->
    
    </article><!-- #post-## -->
    

    (来源:github.com/Conutant/TESSERACT/blob/Master_Branch/content-single.php)

    4.- 要移除特色图片背景,请将其更改为:

    <?php
    /**
    * @package Tesseract
    */
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
      <header class="entry-header">
            <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
      </header><!-- .entry-header -->
    
      <div class="entry-content">
        <div class="entry-meta">
            <?php tesseract_posted_on(); ?>
        </div><!-- .entry-meta -->
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'tesseract' ),
                'after'  => '</div>',
            ) );
        ?>
      </div><!-- .entry-content -->
    
    </article><!-- #post-## -->
    

    5.- 请记住,当您将主题更新到新版本时,所有更改都将被覆盖。避免这种情况的一个好选择是创建子主题,这是修改现有主题的推荐方法。更多信息请访问:https://codex.wordpress.org/Child_Themes

    6.-测试一下,如果有任何问题,请告诉我。

    问候。

    【讨论】:

      【解决方案5】:

      在你的代码中有一行

      <?php get_template_part( 'content', 'single' ); ?>
      

      这意味着它调用你当前主题的 content.php 文件。转到该文件并注释以下代码

      <?php if ( has_post_thumbnail() && ! post_password_required() && ! is_attachment() ) : ?>       <div class="entry-thumbnail">           <?php the_post_thumbnail(); ?>      </div>      <?php endif; ?>
      

      但请注意,当 content.php 文件调用时,它会在任何地方删除缩略图,因此更好的主意是创建自定义 single.php 文件。为此,您需要复制 single.php 文件并用您的帖子名称重命名。 例如,如果您使用 post 添加内容,则使用 single-post.php 重命名,或者如果您使用自定义帖子类型(如新闻),则使用 single-news.php 重命名。

      然后打开此文件并删除此代码

      <?php get_template_part( 'content', 'single' ); ?>
      

      从文件中转到 content.php 文件并复制您要显示的所需代码并粘贴到新文件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 2019-07-31
        • 2012-09-03
        • 2015-04-20
        相关资源
        最近更新 更多