Php 是否在函数中包含w_缩略图\u src?
我得到了一个函数,可以在帖子中自动创建一个自定义字段。我在我的functions.php中找到了这个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'
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)你的意思是什么?这是我的想法,但我已经尝试过了,但没有效果。它只返回一个空值字段。我需要缩略图的网址出现在那里感谢尝试,但它没有工作,但我已经张贴了与yuor helt的答案。谢谢谢谢你的尝试,但它没有工作,但我已经张贴了与yuor helt的答案。谢谢