9

いくつかの投稿 ID があり、これらの投稿の注目の画像を同じ URL から設定したいと考えています。

これが私の追加の郵便番号です:

$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 アイキャッチ画像を example.com/image.png に設定したい

これどうやってするの?

4

3 に答える 3

27

画像がメディア ライブラリにある場合、画像を特集記事のサムネイルとして設定できます。メディア ライブラリに画像を追加するには、サーバーにアップロードする必要があります。

このコードを試してください:

// 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/

要件として変更: または、その目的は WordPress 標準を無視し、すべての投稿の単一の画像をカスタム フォルダーにアップロードし、この画像パスまたは直接の外部画像 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?>">

WordPress 投稿のカスタム メタ フィールドを初めて使用する場合は、次の記事をお読みください: https://codex.wordpress.org/Custom_Fields
または
カスタム フィールドに関する非公式記事: https://premium.wpmudev.org/blog/creating-custom-フィールド - 手動

于 2017-01-07T18:37:40.147 に答える
17

画像を読み込んで投稿のサムネイルとして割り当てる必要がある場合は、media_sideload_image を使用してから set_post_thumbnail を使用する方が簡単です

https://developer.wordpress.org/reference/functions/media_sideload_image/

$url     = "http://url-of-the-image.jpg";
$post_id = [post id]
$desc    = "image description";

$image = media_sideload_image( $url, $post_id, $desc,'id' );

set_post_thumbnail( $post_id, $image );
于 2021-01-25T17:12:05.840 に答える
0

@Narajの回答を補完するために、外部ファイルで作業している場合は、忘れずに追加してください:

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

これを追加しないと、media_sideload_image が定義されていないというエラー メッセージが表示されます。

于 2022-01-31T16:54:49.963 に答える