我有一个名为“投资组合”的自定义帖子类型,我使用脚本设置了特色图片,该脚本拍摄了一些我希望每天使用wordpress.org服务更新的网站的快照
http://s.wordpress.com/mshots/v1/' . urlencode($site) . '?w=' . $width;
脚本运行后,我在媒体库中找到图像并且它们似乎附在帖子上,但如果我检查帖子,则没有任何特色图像,图像也不会显示在前面结束。 我在这里发布我正在使用的脚本,我希望有人可以帮助我。
<?php
function ss_screenshot_action($post_id){
$width = 612;
$site = trim(get_post_meta($post_id, 'site_url', true));
if ($site != ''){
$query_url = 'http://s.wordpress.com/mshots/v1/' . urlencode($site) . '?w=' . $width;
$imagename= str_replace('%2F','',str_replace('.','-', substr(substr(basename($query_url), 13), 0, -6))). '.jpg';
$save_path = ABSPATH.'wp-content/uploads/site_snapshots/';
$save_img_path = ABSPATH.'wp-content/uploads/site_snapshots/'.$imagename;
// first time, the image doesn't exist
if(!file_exists($save_img_path)) {
$image = getimg($query_url);
file_put_contents($save_img_path, $image);
//is the image bigger than 10KB? (cicle and wait for the image to be generated)
while (filesize($save_img_path) < 10000) {
unlink($save_img_path);
sleep(1);
$image = getimg($query_url);
file_put_contents($save_img_path, $image);
}
//insert the attachment
saveSiteImg($save_img_path);
}
//if the image exist and it is older than a day, take it again and change only the image for the attachment - 1day = 86400 = 24*3600 seconds
if(file_exists($save_img_path) && get_post_thumbnail_id($post_id) != 0 && (time() - filemtime($save_img_path)) > 86400) {
$image = getimg($query_url);
file_put_contents($save_img_path, $image);
while (filesize($save_img_path) < 10000) {
unlink($save_img_path);
sleep(1);
$image = getimg($query_url);
file_put_contents($save_img_path, $image);
}
//maybe here I have to update the metadata for the attachment and to regenerate the thumbnails
}
}
}
add_action( 'save_post', 'ss_screenshot_action' );
function getimg($url) {
$headers[] = 'Accept: image/jpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
//Required for http(s)
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
//
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
//curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_BINARYTRANSFER,1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
function saveSiteImg($filename) {
// $filename should be the path to a file in the upload directory.
global $post;
// The ID of the post this attachment is for.
$parent_post_id = $post->ID;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
}
编辑1 以下是我声明自定义帖子类型的地方:
function create_portfolio() {
$labels = array(
'name' => __('Portfolio' , 'portfolio-plugin'),
'singular_name' => __('Portfolio' , 'portfolio-plugin'),
'add_new' => __('Aggiungi testata', 'portfolio-plugin'),
'add_new_item' => __('Aggiungi nuova testata' , 'portfolio-plugin'),
'edit_item' => __('Modifica testata', 'portfolio-plugin'),
'new_item' => __('Nuova testata', 'portfolio-plugin'),
'all_items' => __('Tutte le testate', 'portfolio-plugin'),
'view_item' => __('Vedi le testate' , 'portfolio-plugin'),
'search_items' => __('Cerca testata' , 'portfolio-plugin'),
'not_found' => __('Nessuna testata inserita', 'portfolio-plugin'),
'not_found_in_trash' => __('Work Not found in the trash', 'portfolio-plugin'),
);
$args = array(
'labels' => $labels,
'public' => true,
'rewrite' => array('slug' => 'portfolio'),
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 22,
'menu_icon' => 'dashicons-welcome-write-blog',
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'page-attributes'
),
);
register_post_type('portfolio', $args);
}
答案 0 :(得分:0)
使用'supports' => array( 'thumbnail' )
支持参数:
register_post_type( $post_type, $args );
Arg1T