通过PHP创建新帖子,包括WordPress中的外部特征图像

时间:2018-01-18 13:24:24

标签: php wordpress

我听说这是不可能的,但无论如何我不得不问。

我使用PHP在WP中创建新帖子,如下所示:

<?php
    $postType = 'post'; // set to post or page
    $userID = 1; // set to user id
    $categoryID = '4'; // set to category id.
    $postStatus = 'publish';  // set to future, draft, or publish

    $leadTitle = $post_title;

    $leadContent = $post_content;

    /*******************************************************
    ** TIME VARIABLES / CALCULATIONS
    *******************************************************/
    // VARIABLES
    $timeStamp = $minuteCounter = 0;  // set all timers to 0;
    $iCounter = 1; // number use to multiply by minute increment;
    $minuteIncrement = 1; // increment which to increase each post time for future schedule
    $adjustClockMinutes = 0; // add 1 hour or 60 minutes - daylight savings

    // CALCULATIONS
    $minuteCounter = $iCounter * $minuteIncrement; // setting how far out in time to post if future.
    $minuteCounter = $minuteCounter + $adjustClockMinutes; // adjusting for server timezone

    $timeStamp = date('Y-m-d H:i:s', strtotime("+$minuteCounter min")); // format needed for WordPress

    /*******************************************************
    ** WordPress Array and Variables for posting
    *******************************************************/

    $new_post = array(
        'post_title' => $leadTitle,
        'post_content' => $leadContent,
        'post_status' => $postStatus,
        'post_date' => $timeStamp,
        'post_author' => $userID,
        'post_type' => $postType,
        'post_category' => array($categoryID)
        );

//  /*******************************************************
//  ** WordPress Post Function
//  *******************************************************/

    $post_id = wp_insert_post($new_post);

?>

我还需要包含一张图片,比如说来自其他网站,并将其设置为特征图片。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

尝试使用此代码:

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

代码取自this article