显示或不显示特色图片的选项
Option to display Featured Image, or not
WordPress 4.3.1
嗨,
这是我第一次 post 在 Whosebug 上,我希望我做的一切都正确。
我需要一个允许或不输出特色图片的功能。只是不上传图片是不行的,因为在任何情况下都需要摘录旁边的缩略图。
需要以下组合:
- 缩略图和特色图片相同
- 文章中的缩略图和第一张图片不同
- 缩略图,无特色图片
我需要案例 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 片段,我在输出模板中使用:
<?php the_post_thumbnail('header-image'); ?>
我很感激任何建议!预先感谢您的努力!
首先我会在数据库中检查这个 'optional_featured_image' 参数实际上是由您的代码设置的。检查 prefix_postmeta
table 并查找您正在编辑的 post。
输出模板时,您需要有一个条件来检查您设置的 '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');
?>
WordPress 4.3.1
嗨,
这是我第一次 post 在 Whosebug 上,我希望我做的一切都正确。
我需要一个允许或不输出特色图片的功能。只是不上传图片是不行的,因为在任何情况下都需要摘录旁边的缩略图。
需要以下组合:
- 缩略图和特色图片相同
- 文章中的缩略图和第一张图片不同
- 缩略图,无特色图片
我需要案例 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 片段,我在输出模板中使用:
<?php the_post_thumbnail('header-image'); ?>
我很感激任何建议!预先感谢您的努力!
首先我会在数据库中检查这个 'optional_featured_image' 参数实际上是由您的代码设置的。检查 prefix_postmeta
table 并查找您正在编辑的 post。
输出模板时,您需要有一个条件来检查您设置的 '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');
?>