Php WordPress精选图像|所有与图像文件名匹配的帖子段塞

Php WordPress精选图像|所有与图像文件名匹配的帖子段塞,php,wordpress,Php,Wordpress,我在一个博客上有一堆旧帖子,我想给它们分配一张特色图片 我已经检索了我想在每篇文章中使用的所有图像 我在每篇文章的开头之后保存了每个图像文件名 我想检索所有帖子,从每个帖子中获取slug名称,搜索上传图像文件的指定目录,当post slug与图像文件名匹配时,将该图像设置为该特定帖子的特色图像,并遍历所有帖子 我不知道如何去做这件事,但我提供了一些我发现的示例代码,以及一些有用的(希望)链接 以下代码用于检索所有帖子并更新特定帖子: $allPosts = get_posts(array('nu

我在一个博客上有一堆旧帖子,我想给它们分配一张特色图片

我已经检索了我想在每篇文章中使用的所有图像

我在每篇文章的开头之后保存了每个图像文件名

我想检索所有帖子,从每个帖子中获取slug名称,搜索上传图像文件的指定目录,当post slug与图像文件名匹配时,将该图像设置为该特定帖子的特色图像,并遍历所有帖子

我不知道如何去做这件事,但我提供了一些我发现的示例代码,以及一些有用的(希望)链接

以下代码用于检索所有帖子并更新特定帖子:

$allPosts = get_posts(array('numberposts' => -1, 'post_type' => 'post'));
foreach($allPosts as $thePost){
    $my_post = array();
    $my_post['post_type'] = $thePost->post_type;
    $my_post['ID'] = $thePost->ID;
    $my_post['post_name'] = autoSlug($thePost->post_title);
    wp_update_post($my_post);
}
注意:我有一个特殊的函数,用于根据post\u标题生成post slug。(我不使用WP默认slug。)

有用的链接:

$allPosts = get_posts(array('numberposts' => -1, 'post_type' => 'post'));
foreach($allPosts as $thePost){
    $my_post = array();
    $my_post['post_type'] = $thePost->post_type;
    $my_post['ID'] = $thePost->ID;
    $my_post['post_name'] = autoSlug($thePost->post_title);
    wp_update_post($my_post);
}

  • 我自己写了一个脚本,还有一篇博客文章。对改进的贡献表示感谢。请参阅此链接以获取答案:


    你救了我的命,你的代码只需要一些调整,就像一个魔咒。给你

    <?php
    // Get All Posts.
    $allPosts = get_posts(array('numberposts' => -1, 'post_type' => 'wpdmpro'));
    // Specify where the images are located.
    $themePATH = get_theme_root().'/'.get_stylesheet().'/thumbs/';
    // The uploads directory for your blog.
    $uploads= wp_upload_dir();
    // List of images including extensions.
    $images = listImages($themePATH,true);
    // List of images without extensions.
    $imageNames = listImages($themePATH,false);
    
    function reverseSlug($string){
        $string = str_replace("-", " ", $string);// Convert hyphen to space
        $string = ucwords($string);// Capitalize the beginning of each word
        return $string;
    }
    
    // Retrieve all images from the specified directory.
    // Output array with and without file extensions.
    function listImages($dirname=".",$display) {
        $ext = array("jpg", "png", "jpeg", "gif");
        $files = array();
        if($handle = opendir($dirname)){
            while(false !== ($file = readdir($handle))){
                for($i=0;$i<sizeof($ext);$i++){
                    if(strstr($file, ".".$ext[$i])){
                        $files[] = $file;
                    }
                }
            }
            closedir($handle);
        }
        sort($files);
        foreach($files as $theFile){
            $info = pathinfo($theFile);
            $fileName = basename($theFile,'.'.$info['extension']);
            $files1[] = $fileName;
        }
        if($display == false){
            return ($files1);
        }
        if($display == true){
            return($files);
        }
    }
    
    
    for($i = 0; $i < count($allPosts); $i++){
        // Check if post slugs match image slugs.
        if (is_array($imageNames)) {
            $check[$i] = in_array($allPosts[$i]->post_name, $imageNames);
        } else {
            echo 'error';
        };
    
    
        if($check[$i] == 1){
            echo 'Yes, post title matches image name.<br />'.PHP_EOL;
            // Search through the image slugs for a direct match with the post slug.
            $search[$i] = array_search($allPosts[$i]->post_name, $imageNames);
            $filename = $images[$search[$i]];
            $newfile = $uploads['path'].'/'.$filename;
            // Copy the image from theme folder to uploads directory.
            copy($themePATH.$filename, $newfile);
            // Delete image from theme folder.
            unlink($themePATH.$filename);
            // Retrieve the file type from the file name.
            $wp_filetype = wp_check_filetype(basename($filename), null);
            // Construct the attachment array.
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'guid' => $uploads['url'].'/'.$filename,
                'post_title' => preg_replace('/\.[^.]+$/', '', reverseSlug(basename($filename))),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            // This function inserts an attachment into the media library.
            $attach_id = wp_insert_attachment($attachment, $newfile, $allPosts[$i]->ID);
            // You must first include the image.php file
            // For the function wp_generate_attachment_metadata() to work.
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            // This function generates metadata for an image attachment.
            // It also creates a thumbnail and other intermediate sizes
            // of the image attachment based on the sizes defined on
            // the Settings_Media_Screen.
            $attach_data = wp_generate_attachment_metadata($attach_id, $newfile);
            if(!is_wp_error($attach_id)){
                // Update metadata for an attachment.
                wp_update_attachment_metadata($attach_id, $attach_data);
                // Updates the value of an existing meta key (custom field) for the specified post.
                update_post_meta($allPosts[$i]->ID, '_thumbnail_id', $attach_id);
            }
        }
        else{
            echo 'No matches found.<br />'.PHP_EOL;
        }
    }
    ?>