Wordpress wp.media特色图片ID

时间:2022-06-18 06:07:20

How can I hook into an existing wp.media object and grab the attachment ID when the "Set Featured Image" button is clicked?

如何在单击“设置特色图像”按钮时挂钩到现有的wp.media对象并获​​取附件ID?

The wp.media tutorials I've been looking at all seem to start by creating a new wp.media frame, but I just want to listen for events coming from an existing one (rendered by wp_editor() function), particularly the "Set Featured Image" event.

我一直在看的wp.media教程似乎都是从创建一个新的wp.media框架开始的,但我只是想监听来自现有的事件(由wp_editor()函数呈现),特别是“Set”特色图片“活动。

1 个解决方案

#1


Try using the wp.media.featuredImage object, and more specifically its frame() and get() methods:

尝试使用wp.media.featuredImage对象,更具体地说是它的frame()和get()方法:

// on featured image selection...
wp.media.featuredImage.frame().on( 'select', function(){

    // ...get the attachment ID
    var attachment_id = wp.media.featuredImage.get();

    console.log( attachment_id );

});

#1


Try using the wp.media.featuredImage object, and more specifically its frame() and get() methods:

尝试使用wp.media.featuredImage对象,更具体地说是它的frame()和get()方法:

// on featured image selection...
wp.media.featuredImage.frame().on( 'select', function(){

    // ...get the attachment ID
    var attachment_id = wp.media.featuredImage.get();

    console.log( attachment_id );

});