It's hard to give an exact answer without knowing how your template is marked up, but I think you'll have two options of doing this. Either use a plugin (Advanced Custom Fields), or the solution you mentioned of adding a meta box to the post editor.
I would recommend Option 1 because the plugin is stable, configurable, and will save you some time if you need to add more fields.
Option 1: Use Advanced Custom Fields
Download and install the plugin from their site: https://www.advancedcustomfields.com
Use the plugin to add a custom field. These directions might help: https://www.advancedcustomfields.com/resources/creating-a-field-group/
Make note of the Field Name as you will need this in your code. You might use something such as image_placement. I'd recommend setting up a select box with values such as Left, Center, and Right.
Then, in your PHP template file, you'll want something like this:
<?php if( get_option('image_placement') === 'left' ) : ?> // Write the markup here when the image be left-aligned <?php else if( get_option('image_placement') === 'right' ) : ?> // Write the markup here when the image should be right-aligned <?php else : ?> // Write the markup here when the image should be shown as a banner <?php endif; ?>
Option 2: Add a custom meta box to each post
Add the following code to your functions.php:
// add the meta box to the post editor page function add_image_meta_box( $post ) { add_meta_box( 'image_meta_box', '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 = get_post_meta( $post->ID, '_post_image_placement' ); ?> <h3>Image Placement URL</h3> <select name="image_placement"> <option value="left" <?php ($image_placement[0] === 'left') ?; echo 'selected'; ?>>Left</option> <option value="center" <?php ($image_placement[0] === 'center') ?; echo 'selected'; ?>>Center</option> <option value="right" <?php ($image_placement[0] === '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' );
This will add a field to each of your posts which can be used in the markup like thus:
<?php $image_placement = get_post_meta( $post->ID, '_post_image_placement' )[0]; ?> <?php if( $image_placement === 'left' ) : ?> // Write the markup here when the image be left-aligned <?php else if( $image_placement === 'right' ) : ?> // Write the markup here when the image should be right-aligned <?php else : ?> // Write the markup here when the image should be shown as a banner <?php endif; ?>