1

我有一个关于使用 csv 导入在 wordpress 中导入帖子的问题。我已经像这样“post_title”“post_content”“featured_image”等映射了csv。所有帖子均已导入,但未显示特色图片。

特色图片网址仅显示在自定义字段中。我的问题是,如何让特色图片显示?因为我有数百个帖子,我无法手动编辑它们。

4

2 回答 2

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);
    }
}
于 2018-10-13T15:51:07.687 回答
0

您必须创建一个新的 single-post.php 模板并为自定义字段添加 php。您可以在子主题中添加新的 single-post.php 模板,这样它就不会被主题更新覆盖。

您可以使用此自定义帖子生成器来帮助您入门。首先调用子主题中的任何内容。因此,在您的新自定义 single-Post.php(但称其为 custom-Post.php 或类似名称)中添加逻辑(如果...则)以呈现主题 single-post.php。

然后在您的新 custom-Post.php 中添加您希望显示图像的位置:

**<img src="<?php the_field('advanced_custom_field_name'); ?>" alt=""/>**.

custom-Post.php 将为 html 添加图像标签,高级自定义字段将填写 URL。

或者,您的主题中可能有一个设置,允许您在单个帖子显示中呈现特色图像。

于 2013-10-31T16:17:32.323 回答