简体   繁体   English

获取自定义附件metabox图像Wordpress

[英]Get custom attachment metabox image Wordpress

I have added my custom field to the attachments image metabox. 我已将我的自定义字段添加到附件图像元框。

function be_attachment_field_credit( $form_fields, $post ) {

    $form_fields['img-page-url'] = array(
        'label' => 'Link to page',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'img-page-url', true ),
    );

    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );

function be_attachment_field_credit_save( $post, $attachment ) {
    if( isset( $attachment['img-page-url'] ) )
        update_post_meta( $post['ID'], 'img-page-url', $attachment['img-page-url'] );

    return $post;
}

How can i use this attachment and output it on a page ? 如何使用此附件并将其输出到页面上?

So, i guess that you need to get that attachment, and you have only one key ID, that is the post ID. 因此,我想您需要获取该附件,并且只有一个密钥ID,即帖子ID。 If it is so, then you need to do these steps: 如果是这样,那么您需要执行以下步骤:

  1. img-page-url meta of the given post. 给定帖子的img-page-url元。
  2. finding corresponding attachment by this meta (because they have common meta) 通过此meta查找相应的附件(因为它们具有共同的meta)
  3. getting an attachment by the given ID. 通过给定的ID获取附件。

Let's code it: 让我们编写代码:

<?php 
 $common_meta=get_post_meta( $post->ID, 'img-page-url', true );
 global $wpdb;
 $attachment_id=$wbdp->get_var("select post_id from $wpdb->post_meta where meta_key='img-page-url' and meta_value='".esc_sql($common_meta)."' and post_id!=".$post->ID);
 echo wp_get_attachment_image($attachment_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM