php 如何通过url编程设置特色图像?

jjhzyzn0  于 2023-04-28  发布在  PHP
关注(0)|答案(3)|浏览(127)

我有一些职位ID的,我想设置这些职位的特色图片从同一网址。
以下是我添加的邮政编码:

$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我想将特色图像设置为: www.example.com
我该怎么做?

0x6upsns

0x6upsns1#

当图像在媒体库中时,您可以将其设置为发布功能缩略图。要在媒体库中添加图像,您需要将其上传到服务器。
试试这个代码:

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

ref url:http://www.wpexplorer.com/wordpress-featured-image-url/
根据您的要求修改:或者,目的忽略WordPress的标准,并上传您的所有后单一的图像在您的自定义文件夹,并添加此图像路径或直接外部图像的URL到后作为额外的属性 meta字段,当你将显示后,你的主题,然后只需使用您的img与帮助的职位ID。演示代码:用于设置图像

<?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帖子自定义 meta字段的新手,然后阅读这篇文章:https://codex.wordpress.org/Custom_Fields

关于自定义字段的非官方文章:https://premium.wpmudev.org/blog/creating-custom-fields-manually

oymdgrw7

oymdgrw72#

如果你必须加载一个图像,然后指定为文章的缩略图,更快的方法是使用media_sideload_image,然后设置_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 );
h7appiyu

h7appiyu3#

只是为了补充@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_siload_image未定义。

相关问题