Php WordPress自定义元数据库不保存数据或显示

Php WordPress自定义元数据库不保存数据或显示,php,wordpress,Php,Wordpress,经过两天的研究和没有答案的提问,今天有人提出了一种方法,可以在Wordpress图像中添加一个自定义的metabox字段。我的工作范围是在上传或修改图像时添加URL,目前我只能显示输入字段,用户可以在其中添加URL,但不会保存输入,我无法将其返回前端 我想在get\u post\u gallery()中使用此自定义元盒函数,该函数将输出我在库中添加的图像的URL,但我不知道如何组合此函数,我需要修复插入URL的保存。有人能帮助我或指导我完成实施吗 <?php function add_i

经过两天的研究和没有答案的提问,今天有人提出了一种方法,可以在Wordpress图像中添加一个自定义的metabox字段。我的工作范围是在上传或修改图像时添加URL,目前我只能显示输入字段,用户可以在其中添加URL,但不会保存输入,我无法将其返回前端

我想在
get\u post\u gallery()
中使用此自定义元盒函数,该函数将输出我在库中添加的图像的URL,但我不知道如何组合此函数,我需要修复插入URL的保存。有人能帮助我或指导我完成实施吗

<?php
 function add_image_link()
 {
   add_meta_box(
     'image_link_box',
     'Link image to url',
     'image_link_field',
     'attachment'
   ); 
 }
 add_action( 'add_meta_boxes_attachment', 'add_image_link' );

 function image_link_field( $post )
 {
   ?>
   <label for="image-link">Link URL</label>
   <input type="text" name="image_link" id="image-link" class="postbox">
   <?php
 }

 function save_image_link( $post_id )
 {
   if( array_key_exists( 'image_link', $_POST ) ){
     update_post_meta(
       $post_id,
       'image_link_url',
       $_POST['image_link']
     );
   }

 }
 add_action( 'save_post_attachment', 'save_image_link' );
?>

链接URL

要保存和显示输入字段值(图像链接url),请尝试下面的代码

function image_link_field( $post )
 {
   $url = get_post_meta($post->ID,'image_link_url',true)
   ?>
   <label for="image-link">Link URL</label>
   <input type="text" name="image_link" id="image-link" value="<?php ($url)? $url : '';?>" class="postbox">
   <?php
 }

 function save_image_link( $post_id )
 {
    $image_link = isset( $_POST['image_link'] ) ? $_POST['image_link'] : false;
    update_post_meta( $post_id, 'image_link_url', $image_link );
    return;
 }
 add_action( 'edit_attachment', 'save_image_link' );
函数图像链接字段($post)
{
$url=get\u post\u meta($post->ID,'image\u link\u url',true)
?>
链接URL

试试这个,它工作正常(自定义元框不保存数据或显示在wordpress中)

函数添加图像链接(){
添加元框(
“图像链接框”,
__(“标题:”,“将图像链接到url”),
“图像链接字段”,
['post','page']
);
}
添加操作('add_meta_box_attachment'、'add_image_link');
函数图像链接字段($post){
//添加一个nonce字段,以便稍后检查。
wp_nonce_字段('custom_nonce','custom_nonce');
$value=get\u post\u meta($post->ID,'image\u link\u box',true);
回显“链接URL”;
回声';
}
功能保存图像链接($post\u id)
{
如果(数组\u键\u存在('image\u link\u box',$\u POST)){
更新\u post\u meta(
$post_id,
“图像链接框”,
$\u POST['image\u link\u box']
);
}
}
添加操作(“保存帖子”、“保存图片”链接);

我在保存信息和使用自定义快捷码显示多媒体资料的图像时遇到问题,这是因为快捷码将加载所有媒体库附件,而不仅仅是页面或post附加的媒体。我只需要将其用于图像wp
$screen
,使用
['post',page']
将为帖子和页面添加自定义元,这对我的范围没有用处。请添加屏幕快照。我需要自定义元,仅用于贴在帖子或页面上的图像,而不用于页面和帖子,您的示例将向帖子和页面添加元,但这不是我需要的,我要添加的url字段仅用于r图片:)请检查此链接HI sisaln我认为您的问题已解决。如果有任何问题,请让我知道
function add_image_link() {

add_meta_box(
    'image_link_box',
    __( 'Title:', 'Link image to url' ),
    'image_link_field',
    ['post', 'page']
);
}

add_action( 'add_meta_boxes_attachment', 'add_image_link' );


function image_link_field( $post ) {

// Add a nonce field so we can check for it later.
wp_nonce_field( 'custom_nonce', 'custom_nonce' );

 $value = get_post_meta( $post->ID, 'image_link_box', true );
echo '<label for="image_link_box">Link URL</label>';
echo '<input type="text" value="' . esc_attr( $value ) . '" name="image_link_box" id="image_link_box" class="postbox">';

}


function save_image_link($post_id)
{
if (array_key_exists('image_link_box', $_POST)) {
    update_post_meta(
        $post_id,
        'image_link_box',
        $_POST['image_link_box']
    );
}
}
add_action('save_post', 'save_image_link');