customization - How to allow multiple thumbnail upload for Posts?

admin2025-06-06  1

Here's the deal: I was migrating the content (lots of posts) from an old site to a new one. The posts have multiple images attached to it (which I've noticed when I was importing the thumbnail images via WP All Import) and it is being nicely displayed in single.php template like this:

Main image

<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'full'); ?>
+ HTML markup

Little thumbnails beneath main image (if there are more than one images)

$images = sql_post_images( $ID = get_the_ID(), get_post_thumbnail_id( $ID ) );
foreach($images as $image) {
  HTML markup
}

sql_post_images() function

function sql_post_images( $post_id, $exclude_id = 0 ) {

    global $wpdb;

    $data = $wpdb->get_results(
        $wpdb->prepare(
        "   
            SELECT      DISTINCT wposts.ID, 
                        wposts.post_title,
                        wposts.post_excerpt,
                        wposts.post_content, 
                        wpostmeta.meta_value
            FROM        $wpdb->posts wposts
            LEFT JOIN   $wpdb->postmeta wpostmeta
                        ON wpostmeta.post_id = wposts.ID
                        AND wpostmeta.meta_key = %s
            WHERE       wposts.post_parent = %d
            AND         wposts.post_type = 'attachment'
            AND         (wposts.post_mime_type = 'image/jpeg' OR wposts.post_mime_type = 'image/png' OR wposts.post_mime_type = 'image/gif')
            AND         wposts.ID != %d
            ORDER BY    wposts.menu_order ASC

        ", '_wp_attachment_metadata', $post_id, $exclude_id), ARRAY_A);

    return( $data );
}

This works well and I would like to keep it like that in the future as well - the problem is I cannot find a solution to allow upload of multiple images to a post. I found some plugins, but their approach is clearly different (it's either just a second thumbnail image, or something similar). What could actually work?

EDIT: Uploading images as a classic WP attachments doesn't seem to do the trick.

Here's the deal: I was migrating the content (lots of posts) from an old site to a new one. The posts have multiple images attached to it (which I've noticed when I was importing the thumbnail images via WP All Import) and it is being nicely displayed in single.php template like this:

Main image

<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'full'); ?>
+ HTML markup

Little thumbnails beneath main image (if there are more than one images)

$images = sql_post_images( $ID = get_the_ID(), get_post_thumbnail_id( $ID ) );
foreach($images as $image) {
  HTML markup
}

sql_post_images() function

function sql_post_images( $post_id, $exclude_id = 0 ) {

    global $wpdb;

    $data = $wpdb->get_results(
        $wpdb->prepare(
        "   
            SELECT      DISTINCT wposts.ID, 
                        wposts.post_title,
                        wposts.post_excerpt,
                        wposts.post_content, 
                        wpostmeta.meta_value
            FROM        $wpdb->posts wposts
            LEFT JOIN   $wpdb->postmeta wpostmeta
                        ON wpostmeta.post_id = wposts.ID
                        AND wpostmeta.meta_key = %s
            WHERE       wposts.post_parent = %d
            AND         wposts.post_type = 'attachment'
            AND         (wposts.post_mime_type = 'image/jpeg' OR wposts.post_mime_type = 'image/png' OR wposts.post_mime_type = 'image/gif')
            AND         wposts.ID != %d
            ORDER BY    wposts.menu_order ASC

        ", '_wp_attachment_metadata', $post_id, $exclude_id), ARRAY_A);

    return( $data );
}

This works well and I would like to keep it like that in the future as well - the problem is I cannot find a solution to allow upload of multiple images to a post. I found some plugins, but their approach is clearly different (it's either just a second thumbnail image, or something similar). What could actually work?

EDIT: Uploading images as a classic WP attachments doesn't seem to do the trick.

Share Improve this question asked Oct 29, 2018 at 13:28 Kristián FiloKristián Filo 4316 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Have you considered using the ACF plugin? ACF has a very convenient field type - gallery. I think it would match what you want a 100% without the need to poke around the native/custom DB queries. Check it out.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749224291a317402.html

最新回复(0)