• Resolved legend521

    (@legend521)


    If there are more than or equal to 4 images, call 4 images. If there are less than 4 images, call the first image or feature image as thumbnail. If there is no thumbnail, it is best not to display it. To support external linked pictures.

    like this. The archive list outputs multiple images from the article it belongs to:

    title

    Image 1. Image 2, Image 3, Image 4

    title

    first image (or featured image) …………

    loop output

    Thank you very much for providing such a great theme. As I use the free version, I try not to bother you if possible, but I raised a question here, and no one answered my question.
    Therefore, I have to ask you here.
    I searched all the tutorials on the Internet, but I can’t realize it. This is very simple for you. For my difficult problem, I don’t understand the code, so I ask you for advice. Thank you very much.If it bothers you, I’ll think of another way,

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Legend,

    Personally, I would suggest adding the images as a post meta as well. You can use a plugin like ACF.

    Then, retrieve the images and add them through a hook like: generate_after_entry_title.

    You would need to create a conditional statement to check if the image post meta field has an image (if you have 4, check all), and then display the images depending on the result.

    I think creating code to check for images, in a post might significantly make your site slower in comparison to adding them as post meta.

    It would probably be best to reach out to a developer with regards to this if you’re not familiar with the code.

    Thread Starter legend521

    (@legend521)

    Thank you very much for your guidance. ACF seems to be set in every article. I only know the custom fields of ACF. I didn’t understand the description document by checking the pictures, but I found a lot of code and pieced together the following function fragments. If you see anything that needs to be improved, please help me.

    
    function hui_get_thumbnail( $single=true, $must=true ) {
        global $post;
        $html = '';
        
            $content = $post->post_content;
            preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
            $images = $strResult[1];
            $counter = count($strResult[1]);
            $i = 0;
            foreach($images as $src){
                $i++;
                $src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
                $src2 = $src2[0];
                if( !$src2 && true ){
                    $src = $src;
                }else{
                    $src = $src2;
                }
                $item = sprintf('<li class="item"><img src="%s" alt="' . get_the_title() . '" /></li>', $src);
    			 $item2 = sprintf('<li class="fist-image"><img src="%s" alt="' . get_the_title() . '" /></li>', $src);
                if( $single){
                    return $item;
                    break;
                }
                $html .= $item;
    	if($counter < 4 ){
     return $item2;
                    break;
    }
                if(
                    ($counter >= 4 && $i >= 4) ||
                    ($counter > 0 && $counter < 4 && $i >=$strResult[1][0])
                ){
                    break;
                }
            }
      
        return $html;
    }
    function hui_get_attachment_id_from_src ($link) {
        global $wpdb;
        $link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
        return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");
    }
    • This reply was modified 3 years, 4 months ago by legend521.
    • This reply was modified 3 years, 4 months ago by legend521.
    • This reply was modified 3 years, 4 months ago by legend521.

    I see. It would be best however to start a topic in ACF or WordPress however since this is out of our scope: https://wordpress.org/support/forum/wp-advanced/

    Hope this clarifies!

    Thread Starter legend521

    (@legend521)

    Thank you all the same

    You’re welcome @legend521!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to output multiple thumbnails for category archives’ is closed to new replies.