首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文章中的特色图片、图片和图库中插入和显示版权所有者字段?

如何在文章中的特色图片、图片和图库中插入和显示版权所有者字段?
EN

Stack Overflow用户
提问于 2020-02-10 23:25:25
回答 1查看 39关注 0票数 0

我的要求是为图像提供一个单独的版权字段,并显示每个图像的版权信息。为此,我在基于此源的媒体库中插入了一个单独的字段(https://bavotasan.com/2012/add-a-copyright-field-to-the-media-uploader-in-wordpress/)

代码语言:javascript
复制
/**
 * Adding a "Copyright" field to the media uploader $form_fields array
 *
 * @param array $form_fields
 * @param object $post
 *
 * @return array
 */
function add_copyright_field_to_media_uploader( $form_fields, $post ) {
    $form_fields['copyright_field'] = array(
        'label' => __('Copyright'),
        'value' => get_post_meta( $post->ID, '_custom_copyright', true ),
        'helps' => 'Set a copyright credit for the attachment'
    );

    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_copyright_field_to_media_uploader', null, 2 );

/**
 * Save our new "Copyright" field
 *
 * @param object $post
 * @param object $attachment
 *
 * @return array
 */
function add_copyright_field_to_media_uploader_save( $post, $attachment ) {
    if ( ! empty( $attachment['copyright_field'] ) ) 
        update_post_meta( $post['ID'], '_custom_copyright', $attachment['copyright_field'] );
    else
        delete_post_meta( $post['ID'], '_custom_copyright' );

    return $post;
}
add_filter( 'attachment_fields_to_save', 'add_copyright_field_to_media_uploader_save', null, 2 );

/**
 * Display our new "Copyright" field
 *
 * @param int $attachment_id
 *
 * @return array
 */
function get_featured_image_copyright( $attachment_id = null ) {
    $attachment_id = ( empty( $attachment_id ) ) ? get_post_thumbnail_id() : (int) $attachment_id;

    if ( $attachment_id )
        return get_post_meta( $attachment_id, '_custom_copyright', true );

}

准备好上面的代码后,您现在可以使用以下代码片段在一个页面模板中显示附件的新“版权”字段。

代码语言:javascript
复制
<?php echo get_featured_image_copyright(); ?>

不幸的是,此代码片段仅适用于特色图像。如何在文章中将此代码片段用于图像和图库?这会对代码库起作用吗?如果没有插件,你将如何解决这个问题?如果你能帮忙,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2020-02-11 00:09:48

看起来你在函数get_featured_image_copyright($attachment_id);中有一个参数。

因此,您可以使用图像id调用该函数:

get_featured_image_copyright($image_id);

以图库为例:

代码语言:javascript
复制
while ( have_posts() ) : the_post();
     if ( get_post_gallery() ) :
            $gallery = get_post_gallery( get_the_ID(), false );

            /* Loop through all the image and output copyright one by one */
            foreach( $gallery['ids'] as $id ) : 
              echo get_featured_image_copyright($id);
            endforeach;
     endif;
endwhile;

没有经过测试,但你明白了,我很确定我没有犯任何打字错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60153449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档