【问题标题】:Choose array element or remove the first two选择数组元素或删除前两个
【发布时间】:2014-05-13 00:22:12
【问题描述】:

我正在调整一个生成缩略图的 wordpress 插件。 感谢这个网站,我进步得很漂亮,但我遇到了一个问题。 该插件从帖子中的第一张图片生成缩略图。我需要从第三张图片生成的。

帖子内容

[URL=http://example.com/viewer.php?file=3030128910131424.jpg]      [IMG]http://example.com/images/30301289149131424_thumb.jpg[/IMG][/URL] [URL=http://example.com/viewer.php?file=6331951274718152.jpg][IMG]http://example.com/images/7633127470118152_thumb.jpg[/IMG][/URL]

原代码

// Initialize variable used to store list of matched images as per provided regular expression
$matches = array(); 

// Get all images from post's body
preg_match_all('/\[img\]([^\[\]>]*)/i', $post[0]->post_content, $matches); 

if (count($matches)) {
    foreach ($matches[0] as $key => $image) {
        /**
         * If the image is from wordpress's own media gallery, then it appends the thumbmail id to a css class.
         * Look for this id in the IMG tag.
         */
        preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
        $thumb_id = $thumb_id[1];

        // If thumb id is not found, try to look for the image in DB. Thanks to "Erwin Vrolijk" for providing this code.
        if (!$thumb_id) {
            $image = substr($image, strpos($image, '"')+1);
            $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE guid = '".$image."'");
            $thumb_id = $result[0]->ID;
        }

        // Ok. Still no id found. Some other way used to insert the image in post. Now we must fetch the image from URL and do the needful.
        if (!$thumb_id) {
            $thumb_id = apt_generate_post_thumb($matches, $key, $post[0]->post_content, $post_id);
        }

        // If we succeed in generating thumg, let's update post meta
        if ($thumb_id) {
            update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
            break;
        }
    }
}
}// end apt_publish_post()

 /**
  * Function to fetch the image from URL and generate the required thumbnails
 */

 function apt_generate_post_thumb($matches, $key, $post_content, $post_id)
 { 
 // Make sure to assign correct title to the image. Extract it from img tag
$imageTitle = '';
preg_match_all('/<\s*img [^\>]*title\s*=\s*[\""\']?([^\""\'>]*)/i', $post_content, $matchesTitle);

if (count($matchesTitle) && isset($matchesTitle[1])) {
    $imageTitle = $matchesTitle[1][$key];
}

// Get the URL now for further processing

$imageUrl = $matches[1][$key];
$imageUrl = str_replace("_thumb", "", $imageUrl);

我尝试了两件事

// Get the URL now for further processing

$imageUrl = $matches[3][$key];
$imageUrl = str_replace("_thumb", "", $imageUrl);

不生成缩略图。

现在我试图在获取 url 之前删除前两个数组变量。我试过 array_splice 和 array_slice 但我没有得到任何结果。 从第三个图像而不是第一个图像创建缩略图的任何想法?谢谢!

【问题讨论】:

    标签: php jquery arrays wordpress thumbnails


    【解决方案1】:

    我建议explode($matches),然后在0 1 2 计数中3 上的“第二”元素上运行您的procedure

    【讨论】:

    • 我试了0,2,3,4,5并没有生成缩略图..只有1,我不知道为什么
    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2011-03-09
    • 2021-09-23
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多