我的要求是为图像提供一个单独的版权字段,并显示每个图像的版权信息。为此,我在基于此源的媒体库中插入了一个单独的字段(https://bavotasan.com/2012/add-a-copyright-field-to-the-media-uploader-in-wordpress/)
/**
* 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 );
}准备好上面的代码后,您现在可以使用以下代码片段在一个页面模板中显示附件的新“版权”字段。
<?php echo get_featured_image_copyright(); ?>不幸的是,此代码片段仅适用于特色图像。如何在文章中将此代码片段用于图像和图库?这会对代码库起作用吗?如果没有插件,你将如何解决这个问题?如果你能帮忙,我将不胜感激。
发布于 2020-02-11 00:09:48
看起来你在函数get_featured_image_copyright($attachment_id);中有一个参数。
因此,您可以使用图像id调用该函数:
get_featured_image_copyright($image_id);。
以图库为例:
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;没有经过测试,但你明白了,我很确定我没有犯任何打字错误
https://stackoverflow.com/questions/60153449
复制相似问题