您正接近第二个猜测,但我认为更好的方法是创建自己的自定义字段复选框,该复选框显示在existing featured image meta box. 从可用性的角度来看,这使得功能变得很好和明显,并且可以根据需要逐篇操作。
我把下面的代码作为一个插件放在一起,但出于您的目的,听起来您想将下面的代码(在我的插件注释下面)添加到您的主题函数中。php文件,因为它的功能似乎更适合您的主题。总之,我总结了以下内容:
<?php
/*
* Plugin Name: Featured Image Toggle
* Description: Adds a checkbox to your featured image meta box that you can use to customize how your featured image is displayed
* Version: 1.0
* Author: Ben HartLenn
* Author URI: https://benhartlenn.com
* License: N/A
* Text Domain: featured-image-toggle
*/
// Enable featured images in theme, if they are NOT already enabled
if ( !current_theme_supports(\'post-thumbnails\') ) {
add_theme_support(\'post-thumbnails\');
}
// Add checkbox custom field html to the bottom of the existing featured image meta box in post editor
add_filter(\'admin_post_thumbnail_html\', \'fit_output_featured_image_checkbox\', 10, 2);
function fit_output_featured_image_checkbox($content, $post_id) {
$field_id = \'toggle_featured_image\';
$field_value = esc_attr(get_post_meta($post_id, $field_id, true));
$field_text = esc_html__(\'Show Featured Image in Post Content.\', \'featured-image-toggle\');
$field_state = checked($field_value, 1, false);
$field_label = sprintf(
\'<p><label for="%1$s"><input type="checkbox" name="%1$s" id="%1$s" value="%2$s" %3$s> %4$s</label></p>\', $field_id, $field_value, $field_state, $field_text
);
return $content .= $field_label;
}
// Save checkbox custom field data with value as 1 for checked, and 0 for unchecked
add_action(\'save_post\', \'fit_save_featured_image_checkbox\', 10, 3);
function fit_save_featured_image_checkbox($post_ID, $post, $update) {
$field_id = \'toggle_featured_image\';
$field_value = isset($_REQUEST[$field_id]) ? 1 : 0;
update_post_meta($post_ID, $field_id, $field_value);
}
// Prepend featured image to first paragraph of a posts content, if checkbox is checked, i.e. value == 1
add_filter(\'the_content\', \'fit_insert_featured_image\', 20);
function fit_insert_featured_image($content) {
global $post;
$featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);
if( has_post_thumbnail($post) && $featured_post_toggle == 1 ) {
// NOTE: Adding \'medium\' sized image to post content
$content = preg_replace("/<p>/", "<p>" . get_the_post_thumbnail($post->ID, \'medium\', [\'class\' => \'post-content-featured-image\']), $content, 1);
return $content;
}
}
// If our checkbox is checked, also add a little css to float the image to the right, make the image 33% width of parent element, and thus make it responsive.
add_action( \'wp_head\', \'fit_css\');
function fit_css() {
global $post;
$featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);
if( is_singular(\'post\') && $featured_post_toggle == 1 ) {
echo \'<style type="text/css">\';
// NOTE: Adjust the following css as wanted.
echo \'img.post-content-featured-image { float: right; width: 33%; height: auto; margin: 0 0 2% 2%; }\';
echo \'</style>\';
}
}
听起来您已经有了代码,很可能在主题标题中。php文件,如果设置了该文件,则会显示帖子的特色图像。您需要稍微修改一下,以检查在正常显示特征图像之前是否未选中我们创建的复选框,因此您的新代码可能如下所示:
<?php
$featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);
// $featured_post_toggle == 0 makes sure our checkbox is not checked
if ( is_singular(\'post\') && has_post_thumbnail() && $featured_post_toggle == 0 ) :
?>
<div id="post-image">
<?php the_post_thumbnail(\'large\', [\'class\' => \'post-featured-image\'] ); ?>
</div>
<?php endif; ?>
正如我在评论中所指出的,您可能需要调整帖子内容中特色图像的css,您可以拉入
different sized featured images 当它们显示在帖子内容中时。