2012-02-14 13 views
1

私はブログに私は特色のある画像を割り当てたいと思います古いポストがあります。ワードプレスのおすすめ画像|すべてのポストスラッグ画像ファイル名に一致

私は各投稿に使用したいすべての画像を取得しました。

各画像のファイル名を各投稿のスラッグの後に保存しました。

投稿をすべて取得し、各投稿からスラッグ名を取得し、画像ファイルがアップロードされている指定されたディレクトリを検索し、ポストスラッグが画像ファイル名と一致したときにその画像をその特色画像特定の投稿、すべての投稿を繰り返します。

私はこれを行う方法についてはわかりませんが、いくつかの便利な(うまくいけば)リンクと共に、私が見つけたいくつかのサンプルコードを提供しました。

次のコードは、すべての記事を検索し、特定のポストを更新するために使用されます。

$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_TITLEオフに基づいて、ポストスラグを生成するための特別な機能を持っています。 (私はWPのデフォルトのスラグを使用しないでください。)

便利なリンク:

  1. http://codex.wordpress.org/Template_Tags/get_posts
  2. http://codex.wordpress.org/Function_Reference/wp_update_post
  3. http://codex.wordpress.org/Post_Thumbnails
  4. http://codex.wordpress.org/Function_Reference/wp_upload_dir
  5. http://codex.wordpress.org/Function_Reference/wp_insert_attachment

答えて

2

ブログ記事とともに自分自身でスクリプトを書いた。改善のための貢献が評価されます。答えSet WordPress Featured Image For All Post Slugs Matching Image File Name in Specified Directoryのために、このリンクを参照してください:

<?php 
// Get All Posts. 
$allPosts = get_posts(array('numberposts' => -1, 'post_type' => 'post')); 
// Specify where the images are located. 
$themePATH = get_theme_root().'/'.get_template().'/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. 
    $check[$i] = in_array($allPosts[$i]->post_name, $imageNames); 
    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; 
    } 
} 
?> 
0

あなたは私の命を救う、あなたのコードは、単にいくつかの調整を必要とし、魔法のように動作します。ここにあります。

<?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; 
    } 
} 
?> 
関連する問題