如何使用PHP-Wordpress获取图像附加自定义链接
这将返回到附件的链接:如何使用PHP-Wordpress获取图像附加自定义链接,php,wordpress,image,attachment,Php,Wordpress,Image,Attachment,这将返回到附件的链接: $link=wp_get_attachment_link($image->ID); 但是,我找不到从图像的附件显示设置中获取“链接到”值的方法。请参见下面的屏幕截图 正如yoavmatchulsky所写,手动选择图像后,~wp includes/js/media-views.js会动态归档此字段 但是如果你有一个附件id,使用wp_get_attachment_link$id,$size; 使用“满”作为尺寸 完整参考。或者,如果您试图使用自定义链接,所述
$link=wp_get_attachment_link($image->ID);
但是,我找不到从图像的附件显示设置中获取“链接到”值的方法。请参见下面的屏幕截图
正如yoavmatchulsky所写,手动选择图像后,~wp includes/js/media-views.js会动态归档此字段 但是如果你有一个附件id,使用wp_get_attachment_link$id,$size; 使用“满”作为尺寸
完整参考。或者,如果您试图使用自定义链接,所述方法可能会有所帮助 基本上,在functions.php中,您可以添加类似于以下内容的代码:
// Adds a custom url field to your attachment
function attachment_custom_url( $form_fields, $post ) {
$form_fields['video-url'] = array(
'label' => 'CustomURL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'custom_url', true ),
'helps' => 'Add custom URL, if applicable',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_custom_url', 10, 2 );
function attachment_custom_url_save( $post, $attachment ) {
if( isset( $attachment['custom-url'] ) )
update_post_meta( $post['ID'], 'custom_url', esc_url( $attachment['custom-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'attachment_custom_url_save', 10, 2 );
然后,您可以在php中调用它,如下所示:
<?php
<a href="'.get_post_meta($post->ID, 'custom_url', true).'">Custom Link</a>;
?>
我知道这是一个旧的线程,但我通过获取附件的post-meta来解决这个问题,这对我来说更容易 在我的安装中,自定义URL输入如下所示:
<input type="text" class="text" id="attachments-140443-foogallery_custom_url" name="attachments[140443][foogallery_custom_url]" value="https://mycustomurl.com">
get_post_meta( get_post_thumbnail_id( get_the_ID() ), '_foogallery_custom_url', true);
当然,你需要检查文章是否有缩略图,但这很容易适应 这不是保存的数据,而是在向帖子插入附件时使用的数据。它只影响正在写入日志的标记。