【问题标题】:Option to display Featured Image, or not显示或不显示特色图像的选项
【发布时间】:2015-12-18 08:01:13
【问题描述】:

WordPress 4.3.1

嗨,

这是我在 stackoverflow 上的第一篇文章,我希望我做的一切都正确。

我需要一个允许输出或不输出特色图像的功能。只是不上传图片是不行的,因为无论如何都需要摘录旁边的缩略图。

需要以下组合:

  1. 缩略图和特色图片完全相同
  2. 文章中的缩略图和第一张图片不同
  3. 缩略图和无特色图片

我需要案例2和3的功能。案例2,特色图片将通过编辑器进行整合。

我已经找到了这个插件:https://wordpress.org/plugins/hide-featured-image/

不过,这只能通过 CSS 解决:

.has-post-thumbnail img.wp-post-image{ display: none; }

但我希望图片甚至不被放出并加载到浏览器中。

我在网上找到了以下解决方案。到目前为止,该复选框显示在“特色图像”框中,并且在更新文章后,它还会保存状态(无论是否选中)。但是,精选图像继续输出,我不明白出了什么问题。

这是我复制到functions.php的代码:

/**
 * Adds a checkbox to the featured image metabox.
 *
 * @param string $content
 */
  
add_filter( 'admin_post_thumbnail_html', 'optional_featured_image', 10, 2 );
/**
 * Add a 'hide the featured image' checkbox to the Featured Image area
 */
function optional_featured_image( $content, $post_ID ) {

    $content .= '<div class="hide-feat-image">';
    // add our nonce field
    $content .= wp_nonce_field( 'display_featured_image_nonce', 'display_featured_image_nonce', true, false );
    // create our checkbox
    $content .= '
    <input style="margin: 5px;" type="checkbox" id="display-featured-image" name="display_featured_image" '. checked( get_post_meta( $post_ID, 'display_featured_image', true ), true, false ) .' value="1"/>
    <label for="display-featured-image">Hide on post page?</label>
    ';
    $content .= '</div><!-- hide-feat-image -->';
    // return our appended $content
    return $content;
}

add_action( 'save_post', 'save_optional_featured_image' );
/**
 * Save our 'hide the featured image' checkbox to the Featured Image area
 */
function save_optional_featured_image( $post_id ) {

    if (
        // check nonce
        !isset( $_POST['display_featured_image_nonce'] )
        || !wp_verify_nonce( $_POST['display_featured_image_nonce'], 'display_featured_image_nonce')
        // make sure user is allowed to edit posts
        || !current_user_can( 'edit_post', $post_id )
    )
        return;

    // sanitize our input to yes or no
    $display = isset( $_POST["display_featured_image"] ) && $_POST["display_featured_image"] ? true : false;
    // update our post
    update_post_meta( $post_id, 'display_featured_image', $display );
}  

这是 WP sn-p,我在输出模板中使用:

<?php the_post_thumbnail('header-image'); ?> 

感谢您的任何建议!提前感谢您的努力!

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    首先,我会在数据库中检查这个“optional_featured_image”参数实际上是由您的代码设置的。检查prefix_postmeta 表并查找您正在编辑的帖子。

    输出模板时,您需要有一个条件来检查您设置的“display_featured_image”参数。否则the_post_thumbnail() 将始终显示。

    在您的模板中尝试此代码:

    <?php
    $optional_featured_image = get_post_meta( get_the_ID(), 'display_featured_image', true);
    if( !$optional_featured_image )
        the_post_thumbnail('header-image');
    ?>
    

    【讨论】:

    • 感谢您的回答,詹姆斯!不幸的是,我找不到prefix_postmeta 表。在 phpMyAdmin 中,到达那里的最佳方式是什么?无论如何,我尝试了您的代码 sn-p 并取得了部分成功。当我将它与display_featured_image 一起使用而不是您推荐的optional_featured_image 时,该功能有效,但相反。选中时,会显示特色图片,否则不显示。到目前为止还不错,但应该默认显示特色图片。
    • 编辑了我的答案。我一定是看错了。 “前缀”默认为“wp”,您在安装 WordPress 时设置。所以最有可能的表名是'wp_postmeta'。是的 phpMyAdmin 是一个不错的选择。听起来那个 db 条目无论如何都很好。
    • 在 wp_postmeta 表中存在该条目。值为 1,当 Checkbox 被选中时不显示任何内容,并且默认情况下不选中,如预期的那样。但目前默认情况下,该文章并未显示为特色图片。我怎么能反过来呢?复选框的标签是“隐藏在帖子页面上?”,因此该功能应在单击时隐藏特色图片,而不是在未单击时隐藏。感谢您的帮助!
    • 像我一样在 if 语句中添加感叹号。这表示“如果 $optional_featured_image 不为真,则执行其中的代码”。默认情况下,当用户未选中该框并显示图像时,该变量将为 false。
    • 如果答案解决了您的问题,您能否将问题标记为已解决?
    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2023-03-20
    • 2018-10-23
    • 2017-02-17
    相关资源
    最近更新 更多