Created
April 2, 2020 15:42
-
-
Save prosantamazumder/8a40f551517cc106eaaad87a83f82027 to your computer and use it in GitHub Desktop.
If you use the code then see result add new featured images like main featured image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ADD NEW FEATURED IMAGES META BOXES | |
function listing_images_add_metabox () { | |
add_meta_box( 'listingimagediv', __( 'Home Slide Image', 'text-domain' ), 'listing_images_metabox', 'portfolio', 'side', 'low'); | |
} | |
add_action( 'add_meta_boxes', 'listing_images_add_metabox' ); | |
function listing_images_metabox ( $post ) { | |
global $content_width, $_wp_additional_image_sizes; | |
$image_id = get_post_meta( $post->ID, '_listing_images_id', true ); | |
$old_content_width = $content_width; | |
$content_width = 254; | |
if ( $image_id && get_post( $image_id ) ) { | |
if ( ! isset( $_wp_additional_image_sizes['post-thumbnail'] ) ) { | |
$thumbnail_html = wp_get_attachment_image( $image_id, array( $content_width, $content_width ) ); | |
} else { | |
$thumbnail_html = wp_get_attachment_image( $image_id, 'post-thumbnail' ); | |
} | |
if ( ! empty( $thumbnail_html ) ) { | |
$content = $thumbnail_html; | |
$content .= '<p class="hide-if-no-js"><a href="javascript:;" id="remove_listing_image_button" >' . esc_html__( 'Remove listing image', 'text-domain' ) . '</a></p>'; | |
$content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_images" value="' . esc_attr( $image_id ) . '" />'; | |
} | |
$content_width = $old_content_width; | |
} else { | |
$content = '<img src="" style="width:' . esc_attr( $content_width ) . 'px;height:auto;border:0;display:none;" />'; | |
$content .= '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set listing image', 'text-domain' ) . '" href="javascript:;" id="upload_listing_image_button" id="set-listing-image" data-uploader_title="' . esc_attr__( 'Choose an image', 'text-domain' ) . '" data-uploader_button_text="' . esc_attr__( 'Set listing image', 'text-domain' ) . '">' . esc_html__( 'Set listing image', 'text-domain' ) . '</a></p>'; | |
$content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_images" value="" />'; | |
} | |
echo $content; | |
} | |
add_action( 'save_post', 'listing_images_save', 10, 1 ); | |
function listing_images_save ( $post_id ) { | |
if( isset( $_POST['_listing_cover_images'] ) ) { | |
$image_id = (int) $_POST['_listing_cover_images']; | |
update_post_meta( $post_id, '_listing_images_id', $image_id ); | |
} | |
} | |
============================================================================ | |
<?php | |
//SHOW CUSTOM POST FEATURED IMAGES | |
while( have_posts() ): the_post(); | |
$image_id = get_post_meta( get_the_ID(), '_listing_images_id', true ); | |
$thumbnail_html = wp_get_attachment_image_src( $image_id, 'full' ); | |
?> | |
<img src="<?php echo $thumbnail_html[0]; ?>" alt="" /> | |
<?php endwhile; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment