【发布时间】:2015-12-18 08:01:13
【问题描述】:
WordPress 4.3.1
嗨,
这是我在 stackoverflow 上的第一篇文章,我希望我做的一切都正确。
我需要一个允许输出或不输出特色图像的功能。只是不上传图片是不行的,因为无论如何都需要摘录旁边的缩略图。
需要以下组合:
- 缩略图和特色图片完全相同
- 文章中的缩略图和第一张图片不同
- 缩略图和无特色图片
我需要案例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