[英]How to set featured image programmatically from url?
我有一些帖子 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
我怎樣才能做到這一點?
您可以將位於媒體庫中的圖像設置為發布精選縮略圖。 要在您的媒體庫中添加圖像,您需要將其上傳到您的服務器。
試試這個代碼:
// 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 );
參考網址: http : //www.wpexplorer.com/wordpress-featured-image-url/
根據您的要求進行修改:為此,請忽略 wordpress 標准,將所有帖子的單個圖片上傳到您的自定義文件夾中,並將此圖片路徑或直接外部圖片網址添加到帖子中作為額外的屬性元字段,當您將在您的主題上顯示帖子時,只需在帖子 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-fields-manually
如果您必須加載圖像然后指定為帖子的縮略圖,則更快的方法是使用 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 );
我使用 wordpress 的媒體管理器 wp.media 從插件中的表單上傳特色圖片。 它有它自己的優點,而且非常簡單。
HTML 部分:在 html 表單中,您只需要 2 個元素、一個按鈕和一個隱藏的輸入字段來存儲圖像 ID 並與表單一起發送。 (您可以使用 ajax 進行更新,但在我的情況下,此方法更簡單)
<input type="button" name="select-thumbnail" class="button" Value="Select Thumbnail"> <input type="hidden" name="thumbnail-id" value="">
Javascript (jQuery):在 javascript 上,您需要-
jQuery('input[name="select-thumbnail"]').click(function(e) { e.preventDefault(); var thumbMediaManager; //open if already initialized if(thumbMediaManager) { thumbMediaManager.open(); return; } //create media manafer thumbMediaManager = wp.media({ title: 'Select Thumbnail', multiple: false, library : { type : 'image', } }); //when an image is selected thumbMediaManager.on('select', function() { var selection = thumbMediaManager.state().get('selection').first().toJSON(); //console.log(selection); //insert the id into input field jQuery('input[name="thumbnail-id"]').val(selection.id); //Finally, open the media manager thumbMediaManager.open(); });
Php:提交表單時,檢索圖像附件ID,並獲取帖子ID(在我的情況下來自wp_insert_post),並使用set_post_thumbnail()
$thumbnail_attachment_id = sanitize_text_field($_POST['thumbnail-id']);
$postId = wp_insert_post($newPost);
set_post_thumbnail($postId, $thumbnail_attachment_id);
set_post_thumbnail: https : //developer.wordpress.org/reference/functions/set_post_thumbnail/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.