2017-01-07 8 views
1

일부 게시물 ID가 있고이 게시물의 동일한 URL에서 추천 이미지를 설정하고 싶습니다. 여기추천 이미지를 프로그래밍 방식으로 URL에서 설정하는 방법은 무엇입니까?

내 포스트 코드를 추가 :

나는이 어떻게 할 수있는

example.com/image.png :

$catid = get_cat_ID("XX Cat"); 

$my_post = array(); 
$my_post['post_title'] = $title; 
$my_post['post_content'] = $description; 
$my_post['post_status'] = 'publish'; 
$my_post['post_author'] = 1; 
$my_post['post_category'] = array($catid); 

$post_id = wp_insert_post($my_post); 

예 : 나는 기능을 갖춘 이미지를 설정할 post_id를 = 1?

답변

3

이미지가 미디어 라이브러리에있을 때 미리보기 이미지로 설정할 수 있습니다. 미디어 라이브러리에 이미지를 추가하려면 이미지를 서버에 업로드해야합니다.

// Add Featured Image to Post 
    $image_url  = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // Define the image URL here 
    $image_name  = 'wp-header-logo.png'; 
    $upload_dir  = wp_upload_dir(); // Set upload folder 
    $image_data  = file_get_contents($image_url); // Get image data 
    $unique_file_name = wp_unique_filename($upload_dir['path'], $image_name); // Generate unique name 
    $filename   = basename($unique_file_name); // Create image file name 

    // Check folder permission and define file location 
    if(wp_mkdir_p($upload_dir['path'])) { 
     $file = $upload_dir['path'] . '/' . $filename; 
    } else { 
     $file = $upload_dir['basedir'] . '/' . $filename; 
    } 

    // Create the image file on the server 
    file_put_contents($file, $image_data); 

    // Check image file type 
    $wp_filetype = wp_check_filetype($filename, null); 

    // Set attachment data 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title'  => sanitize_file_name($filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 

    // Create the attachment 
    $attach_id = wp_insert_attachment($attachment, $file, $post_id); 

    // Include image.php 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 

    // Define attachment metadata 
    $attach_data = wp_generate_attachment_metadata($attach_id, $file); 

    // Assign metadata to attachment 
    wp_update_attachment_metadata($attach_id, $attach_data); 

    // And finally assign featured image to post 
    set_post_thumbnail($post_id, $attach_id); 

심판 URL :

http://www.wpexplorer.com/wordpress-featured-image-url/가 귀하의 요구 사항으로 수정 된 항목 : 그 목적을 위해 워드 프레스 기준을 무시하고 사용자 정의 폴더에있는 모든 게시물에 하나의 이미지를 업로드하고이 이미지를 추가

이 코드를 시도 경로 또는 직접 외부 이미지 URL을 추가 속성 메타 필드로 게시하고 테마에 게시물을 표시 한 다음 게시물 ID의 도움으로 img를 사용하십시오. 데모 코드 : 테마 페이지에 이미지를 얻기 위해 이미지

<?php 
    update_post_meta ( 7, 'imgkey', 'www.url.path');//7 is post id 
?> 

을 설정하는 당신이 사용자 정의 메타 필드는 다음이 기사를 읽고 워드 프레스 게시물에 새로운 경우 그것을

<?php 
$img_value = get_post_meta(get_the_ID(), 'imgkey', true); 
?> 
<img src="<?php echo $img_value?>"> 

주를 표시 할 https://codex.wordpress.org/Custom_Fields
또는 사용자 정의 필드에 대한
비공식 기사 : https://premium.wpmudev.org/blog/creating-custom-fields-manually

+0

하나의 이미지 만 사용하고 싶습니다. 그러나이 코드는 하나 이상의 이미지를 추가하고 있습니다 ... 또한 다른 크기의 이미지를 추가하고 싶지 않습니다. 원래 크기 만 이도록 이미지를 업로드하고 싶습니다 ... –

+0

나는 내 대답을 업데이트하고 있습니다. –