简体   繁体   English

如何使用 PHP 获取图像附件自定义链接 - Wordpress

[英]How to get the image attachement custom link with PHP - Wordpress

This will return the link to the attachment:这将返回到附件的链接:

    $link=wp_get_attachment_link($image->ID);

However, I can't find a way to get the LINK TO value from the ATTACHMENT DISPLAY SETTINGS of an image.但是,我找不到从图像的附件显示设置中获取链接到值的方法。 See screenshot below.请参阅下面的屏幕截图。

在此处输入图片说明

As yoavmatchulsky wrote, this field is dynamically filed by ~wp-includes/js/media-views.js after you manually choose an image正如 yoavmatchulsky 所写,在您手动选择图像后,该字段由 ~wp-includes/js/media-views.js 动态归档
but if you have an id of attachment, use wp_get_attachment_link( $id, $size);但是如果你有一个附件 id,请使用 wp_get_attachment_link( $id, $size);
as size use 'full'作为大小使用“完整”
full ref.完整参考in codex手抄本中

Or, if you are trying to use a custom link, the method described here might help.或者,如果您尝试使用自定义链接, 此处描述的方法可能会有所帮助。

Basically, in your functions.php, you could add a code similar to this:基本上,在您的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 );

And then, you could call it in your php like so:然后,您可以像这样在 php 中调用它:

<?php 
<a href="'.get_post_meta($post->ID, 'custom_url', true).'">Custom Link</a>;
?>

I know this is an old thread, but I solve this by getting the post meta of the attachment, which was easier to me.我知道这是一个旧线程,但我通过获取附件的后元来解决这个问题,这对我来说更容易。

In my installation, custom URL input is shown like this:在我的安装中,自定义 URL 输入如下所示:

<input type="text" class="text" id="attachments-140443-foogallery_custom_url" name="attachments[140443][foogallery_custom_url]" value="https://mycustomurl.com">

So, I assumed that if the first brackets contains the post ID, the second one is a meta key to save this value on wp_postmeta table.因此,我假设如果第一个括号包含帖子 ID,则第二个括号是一个元键,用于将此值保存在 wp_postmeta 表中。 And there it was, just starting with the underscore character so it would be a hidden meta data.就在那里,只是从下划线字符开始,所以它将是一个隐藏的元数据。 Therefore, the easier way to get this value is like this:因此,获取此值的更简单方法是这样的:

get_post_meta( get_post_thumbnail_id( get_the_ID() ), '_foogallery_custom_url', true);

Of course you need to check if the post does have a thumbnail, but that's easy to adapt.当然,您需要检查帖子是否有缩略图,但这很容易调整。

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

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