2017-03-29 1 views
0

웹 응용 프로그램의 하위 디렉토리로 호스팅되는 WordPress 블로그가 있습니다. WordPress 설치는 변경되지 않고 테마 레벨에서 수정이 이루어집니다. 웹 응용 프로그램 홈 페이지는 최근 블로그 게시물을 표시해야합니다. 따라서 최근 게시글을 표시하는 코드는 다음과 같습니다.워드 프레스 밖에서 맞춤 크기의 추천 이미지를 받으세요

$array = array(); 

    $query = "SELECT * FROM wp_posts WHERE post_status=:publish AND post_type=:post_type"; 
    $stmt = $this->dbj->prepare($query); 
    $stmt->execute(array(
     ':publish' => 'publish', 
     ':post_type' => 'post' 
    )); 

    if ($stmt->rowCount() > 0) { 
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
      $id = $row["ID"]; 
      if ($row["post_name"] == '') { 
       $slug = $row["ID"]; 
      } else { 
       $slug = $row["post_name"]; 
      } 
      $content1 = strip_tags($row["post_content"]); 

      $image_prepare = $this->dbj->prepare('SELECT 
    wp_postmeta.meta_id, 
    wp_postmeta.post_id, 
    wp_postmeta.meta_key, 
    wp_postmeta.meta_value 
FROM 
    wp_postmeta 
INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id 
WHERE 
    wp_postmeta.meta_key = :meta_key 
AND wp_posts.post_parent = :pid'); 

      $image_prepare->execute(
       array(
        ':pid' => $id, 
        ':meta_key' => '_wp_attached_file' 
       ) 
      ); 

      $image_fetch = $image_prepare->fetchAll(); 

      foreach ($image_fetch as $image_row) { 
       $image_name = $row['post_title']; 
       $image_path = $image_row['meta_value']; 
      } 

      if (strlen($content1) > 200) { 
       $content = substr($content1, 0, 200) . '...'; 
      } else { 
       $content = nl2br($content1); 
      } 

      $array[] = array(
       'image_path' => $image_path, 
       'image_name' => $image_name, 
       'slug' => $row["post_name"], 
       'content' => $content, 
       'title' => $row['post_title'] 
      ); 
     } 
    return $array; 
    } 

전체 크기 이미지 URL을 받고 있습니다. 나는 테마 내 functions.php 파일의 크기 조정을 완료 한 한 :

add_theme_support('post-thumbnails'); 

add_image_size('home-size', 214, 300); 
add_image_size('home-featured', 300, 300); 
add_image_size('jobs-thumb', 50, 75); 

문제는 업로드 디렉토리의 크기가 조정 된 이미지는 항상 home-featured에 대한 image-name-300x300.jpg이없는 것입니다. 대신 파일 이름 (예 : image-name-300x220.jpg)의 이미지 비율에 따라 높이에 임의의 값이 있습니다. 축소판을 다시 생성했지만 동일한 파일 이름을 다시 생성했습니다. 위의 솔루션에 대해 'home-featured'을 얻으려면 어떻게해야합니까?

답변

1

워드 프레스는 _wp_attachment_metadata이있는 postmeta 테이블에 생성 된 이미지의 일련 화 된 데이터를 meta_key로 유지합니다.

코드에 풀 사이즈 이미지가 필요하지 않은 경우 _wp_attached_file_wp_attachment_metadata으로 변경하십시오.

$image_prepare->execute(
    array(
     ':pid' => $id, 
     ':meta_key' => '_wp_attached_file' 
    ) 
); 

$image_fetch = $image_prepare->fetchAll(); 

foreach ($image_fetch as $image_row) { 
    $image_name = $row['post_title']; 
    $image_meta = unserialize($image_row['meta_value']); 
    $image_path = $image_meta['sizes']['home-featured']['file']; 
} 

$image_meta이 될 것입니다 비슷한에 :

array (
    'width' => 720, 
    'height' => 960, 
    'file' => '2017/03/image.jpg', 
    'sizes' => 
     array (
      'thumbnail' => 
       array (
        'file' => 'image-150x150.jpg', 
        'width' => 150, 
        'height' => 150, 
        'mime-type' => 'image/jpeg', 
       ), 
      'medium' => 
       array (
        'file' => 'image-225x300.jpg', 
        'width' => 225, 
        'height' => 300, 
        'mime-type' => 'image/jpeg', 
       ), 
      'twentyseventeen-thumbnail-avatar' => 
       array (
        'file' => 'image-100x100.jpg', 
        'width' => 100, 
        'height' => 100, 
        'mime-type' => 'image/jpeg', 
       ), 
     ), 
관련 문제