Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter legend521

    (@legend521)

    Thank you all the same

    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 2 years, 8 months ago by legend521.
    • This reply was modified 2 years, 8 months ago by legend521.
    • This reply was modified 2 years, 8 months ago by legend521.
Viewing 2 replies - 1 through 2 (of 2 total)