最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - How to allow multiple thumbnail upload for Posts?

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far