我对使用csv import导入wordpress中的帖子有疑问。我已经将csv映射为“post_title”“post_content”“featured_image”等。所有帖子都已导入,但特色图片未显示。
特色图片网址仅显示在自定义字段中。 我的问题是,如何显示特色图片?因为我有数百个帖子,我无法手动编辑它们。
答案 0 :(得分:1)
我遇到了同样的问题–我在多个帖子中都有自定义字段,其中包含要用于特色图片的URL。我知道某些图片已经在我的媒体库中,因此我做了以下功能,将一个批量编辑操作添加到编辑器窗口中,该窗口遍历了帖子并上传了图像,而这些图像已经不在库中了,或已在库中获取图像的ID,然后将其设置为帖子的新精选图像。
这是一个粗略的代码,但是可以正常工作-不过请先备份您的网站。
function does_file_exists($filename) {
global $wpdb;
$data = $wpdb->get_results( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%/$filename'" );
foreach ($data as $ID){
$ID = $ID->post_id;
if (get_post_type($ID) == "attachment"){
return $ID;
}
}
return null;
}
function upload_image_from_URL($image_url, $post_id){
// Add Featured Image to Post
$image_name = pathinfo($image_url)['basename'];
$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 );
return $attach_id;
}
function set_featured_image($post_id){
if (wp_is_post_revision($post_id)) return;
if (has_post_thumbnail($post_id)) return;
$image_url = get_field('FIELD_NAME', $post_id);
if ($image_url){
$attachment_ID = does_file_exists(pathinfo($image_url)['basename']);
$attachment_ID = ($attachment_ID ? $attachment_ID : upload_image_from_URL($image_url, $post_id));
if ($attachment_ID){
$message = "Setting attachment ". $attachment_ID ." to post ". $post_id;
error_log($message);
set_post_thumbnail($post_id, $attachment_ID);
}
}
}
add_filter( 'bulk_actions-edit-post', 'register_bulk_update_featured_image' );
function register_bulk_update_featured_image($bulk_actions) {
$bulk_actions['update_featured_image'] = "Get featured image from field";
return $bulk_actions;
}
add_filter( 'handle_bulk_actions-edit-post', 'bulk_update_featured_image', 10, 3 );
function bulk_update_featured_image( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'update_featured_image' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform action for each post.
set_featured_image($post_id);
}
$redirect_to = add_query_arg( 'bulk_updated_featured_images', count( $post_ids ), $redirect_to );
return $redirect_to;
}
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
function my_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['bulk_updated_featured_images'] ) ) {
$id_count = intval( $_REQUEST['bulk_updated_featured_images'] );
printf('<div id="message" class="updated fade">Updated %s featured images.</div>',
$id_count);
}
}
答案 1 :(得分:0)
您必须创建一个新的single-post.php模板并为自定义字段添加php。您可以在子主题中添加新的single-post.php模板,以便主题更新不会覆盖它。
您可以使用此custom post generator来帮助您入门。首先调用子主题中的任何内容。所以在你的新自定义单一Post.php(但称之为custom-Post.php或类似的东西)中添加逻辑(如果...则)以渲染主题single-post.php。
然后在您的新自定义Post.php中添加此项以显示图像:
**<img src="<?php the_field('advanced_custom_field_name'); ?>" alt=""/>**
。
custom-Post.php将添加html的图像标记,而高级自定义字段将填充URL。
或者,您的主题中可能有一个设置,允许您在单个帖子显示中呈现特色图像。