榮譽給@ rideron89對我的幫助與解決方案。所以在這裏,如果任何人需要使用它(在一些實現中):
我保持functions.php有點乾淨,所以我把它放在'incl'文件夾中的另一個php文件,它被包含在函數中。PHP的」
// add the meta box to the post editor page
function add_image_meta_box($post) {
add_meta_box('image_meta_box', 'Featured Image Placement', 'image_build_meta_box', 'post', 'side', 'low');
}
add_action('add_meta_boxes', 'add_image_meta_box');
// build the front-end for the meta box (shown on the post editor page)
function image_build_meta_box($post) {
wp_nonce_field(basename(__FILE__), 'image_meta_box_nonce');
$image_placement_array = get_post_meta($post->ID, '_post_image_placement');
$image_placement = implode (" ",$image_placement_array);
?>
<p>Please select the layout/alignment of your featured image <em>(default is full width banner)</em></p>
<select name="image_placement">
<option value="default" name="feat_img_align" <?php if($image_placement === 'default'){ echo "selected"; } ?>>Default</option>
<option value="left" name="feat_img_align" <?php if($image_placement === 'left'){ echo "selected"; } ?>>Left</option>
<option value="right" name="feat_img_align" <?php if($image_placement === 'right'){ echo "selected"; } ?>>Right</option>
</select>
<?php
}
// save the setting
function image_save_meta_box_data($post_id) {
// Check the user's permissions.
if (!current_user_can('edit_post', $post_id)) {
return;
}
$image_placement = $_POST['image_placement'];
if(isset($image_placement)){
update_post_meta($post_id, '_post_image_placement', sanitize_text_field($image_placement));
}
}
add_action('save_post', 'image_save_meta_box_data');
,我插入後content.php模板的代碼是:
<?php
$post_feat_img = quick_resize_to_ratio_and_size(get_post_thumbnail_id($post->ID),1,1,250);
$alt_text = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true);
$image_placement_array = get_post_meta($post->ID, '_post_image_placement');
$image_placement = implode (" ",$image_placement_array);
?>
<?php if ($image_placement === 'default') { ?>
<p><?php echo get_the_post_thumbnail($post->ID, 'large', array('class'=>'img-responsive center-block img-thumbnail')); ?></p>
<?php } else if ($image_placement === 'left') { ?>
<img src="<?php echo $post_feat_img; ?>" alt="<?php echo $alt_text ?>" class="alignFeatleft img-thumbnail img-responsive">
<?php } else if ($image_placement === 'right') { ?>
<img src="<?php echo $post_feat_img; ?>" alt="<?php echo $alt_text ?>" class="alignFeatRight img-thumbnail img-responsive">
<?php } else { ?>
<p><?php echo get_the_post_thumbnail($post->ID, 'large', array('class'=>'img-responsive center-block img-thumbnail')); ?></p>
<?php } ?>
<?php the_content(); ?>
我在撲滅模糊的問題的一點,所以很抱歉,我一直在尋找一個方向在我開始編寫代碼之前(可能應該更多地考慮它,並試圖在提問之前對代碼進行編碼)。我考慮了更多,並且發佈metabox似乎是顯而易見的方式,因爲我可以針對每個帖子存儲自定義用戶信息 - 我只是猶豫要劈砍(帖子)主題 - 我之前用CPT做過,不用擔心,我會實現你的建議選項2. –
Just impl發表了你的代碼@ rideron89。只需要收緊一些東西。我通過implode將$ image_placement數組轉換爲字符串(更容易管理)。此外,