Php 是否在函数中包含w_缩略图\u src?

Php 是否在函数中包含w_缩略图\u src?,php,wordpress,function,thumbnails,Php,Wordpress,Function,Thumbnails,我得到了一个函数,可以在帖子中自动创建一个自定义字段。我在我的functions.php中找到了这个 Image是自定义字段的名称,这里的是值。如何将函数w_thumbnail\u src作为变量 add_action('wp_insert_post', 'mk_set_default_custom_fields'); function mk_set_default_custom_fields($post_id) { if ( $_GET['post_type'

我得到了一个函数,可以在帖子中自动创建一个自定义字段。我在我的functions.php中找到了这个

Image
是自定义字段的名称,这里的
是值。如何将函数
w_thumbnail\u src
作为变量

add_action('wp_insert_post', 'mk_set_default_custom_fields');
    function mk_set_default_custom_fields($post_id)

    {
        if ( $_GET['post_type'] != 'post' ) {
            add_post_meta($post_id, 'Image','HERE', true);
        }
        return true;
    }
让我加上
w_thumbnail\u src
是同一个文件中的一个函数,如下所示

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
       echo $thumb[0]; // thumbnail url
    }
}

我认为你需要改变:
add_post_meta($post_id,'Image','HERE',true)
致:
add_post_meta($post_id,'Image',w_thumbnail_src(),true)

并通过将w_thumbnail_src()函数更改为以下内容来修复该函数:

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
        return $thumb[0]; // thumbnail url
    } else {
        return '';  // or a default thumbnail url
    }
}

我认为你需要改变:
add_post_meta($post_id,'Image','HERE',true)
致:
add_post_meta($post_id,'Image',w_thumbnail_src(),true)

并通过将w_thumbnail_src()函数更改为以下内容来修复该函数:

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
        return $thumb[0]; // thumbnail url
    } else {
        return '';  // or a default thumbnail url
    }
}

下面是将缩略图url添加到名为Image的自定义字段的最终代码

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
        return $thumb[0]; // thumbnail url
    } else {
        return '';  // or a default thumbnail url
    }
}


add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}

下面是将缩略图url添加到名为Image的自定义字段的最终代码

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
        return $thumb[0]; // thumbnail url
    } else {
        return '';  // or a default thumbnail url
    }
}


add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}

我不太清楚你在问什么?是
add_post_meta($post_id,'Image',w_thumbnail_src(),true)add_post_meta($post_id,'Image',w_thumbnail_src(),true)