我已经创建了一个自定义帖子类型,并使用这一伟大的资源添加了一些自定义元框:https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
我发现,问题是post元字段有不同的id
取决于职位是否来自第一类类型。我在页面模板上拖动帖子元时遇到问题,原因如下:New WP_query in template not working with CPT+category on some pages, 导致必须引用不同的元框id
对于不同的页面模板,这取决于我拉的类别。
这没什么大不了的,但现在让我想到了我的现状:从嵌入的视频中提取缩略图,并使用这个神奇的插件自动将其设置为特色图像,Featured Image of Video from oembed, 第一个类别有效,但其他类别均无效。一定有什么关系吧?
以下是我的自定义元框代码:
//Initialize the meta boxes
add_action( \'init\', \'wpt_initialize_cmb_meta_boxes\', 9999 );
function wpt_initialize_cmb_meta_boxes() {
if ( ! class_exists( \'cmb_Meta_Box\' ) )
require_once dirname( __FILE__ ) . \'/includes/metaboxes/init.php\';
}
//add custom fields
add_filter( \'cmb_meta_boxes\' , \'wpt_create_metaboxes\' );
function wpt_create_metaboxes( $meta_boxes ) {
// Start with an underscore to hide fields from custom fields list
$prefix = \'_cmb_\';
//Films details meta boxes
$meta_boxes[] = array(
\'id\' => \'film_contents\',
\'title\' => \'Films\',
\'pages\' => array(\'films\'),//Add our post_type() we created earlier.
\'context\' => \'normal\',
\'priority\' => \'low\',
\'show_names\' => true,
\'fields\' => array(
array( //Add video embed field
\'name\' => \'Film URL\',
\'desc\' => \'Enter a youtube or vimeo URL. Supports services listed at <a href="http://codex.wordpress.org/Embeds">http://codex.wordpress.org/Embeds</a>.\',
\'id\' => $prefix .\'film_embed\',
\'type\' => \'oembed\',
),
array( //Add a text area
\'name\' => \'Film Description\',
\'desc\' => \'A few words about the film.\',
\'std\' => \'\',
\'id\' => $prefix .\'film_textarea\',
\'type\' => \'textarea\',
),
)
);
return $meta_boxes;
}
下面是我修改后的“来自oembed的特色图像”代码,用于处理我的元框:
/**
* Plugin Name: oEmbed Featured Image
* Plugin URI: https://wordpress.stackexchange.com/q/70752/1685
* Description: Automatically set the featured image if an oEmbed-compatible embed is found in the post content.
* Version: 1.0
* Author: TheDeadMedic
* Author URI: https://wordpress.stackexchange.com/users/1685/thedeadmedic
*
* @package oEmbed_Featured_Image
*/
add_action( \'wp_insert_post\', array( \'ofi\', \'init\' ) );
/**
* @package oEmbed_Featured_Image
*/
class ofi
{
/**
* The post thumbnail ID
*
* @var int
*/
private $_thumb_id;
/**
* The post ID
*
* @var int
*/
private $_post_id;
/**
* Sets up an instance if called statically, and attempts to set the featured
* image from an embed in the post content (if one has not already been set).
*
* @param int $post_id
* @return object|null
*/
public function init( $post_id )
{
if ( ! isset( $this ) )
return new ofi( $post_id );
global $wp_embed;
$this->_post_id = absint( $post_id );
if ( ! $this->_thumb_id = get_post_meta( $this->_post_id, \'_thumbnail_id\', true ) ) {
if ( $content = get_post_field( \'film_embed\', $this->_post_id, \'raw\' ) ) {
add_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
$wp_embed->autoembed( $content );
remove_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
}
}
}
/**
* @see init()
*/
public function __construct( $post_id )
{
$this->init( $post_id );
}
/**
* Callback for the "oembed_dataparse" hook, which will fire on a successful
* response from the oEmbed provider.
*
* @see WP_oEmbed::data2html()
*
* @param string $return The embed HTML
* @param object $data The oEmbed response
* @param string $url The oEmbed content URL
*/
public function oembed_dataparse( $return, $data, $url )
{
if ( ! empty( $data->thumbnail_url ) && ! $this->_thumb_id ) {
// if ( in_array( @ $data->type, array( \'video\' ) ) ) // Only set for video embeds
$this->set_thumb_by_url( $data->thumbnail_url, @ $data->title );
}
}
/**
* Attempt to download the image from the URL, add it to the media library,
* and set as the featured image.
*
* @see media_sideload_image()
*
* @param string $url
* @param string $title Optionally set attachment title
*/
public function set_thumb_by_url( $url, $title = null )
{
/* Following assets will already be loaded if in admin */
require_once ABSPATH . \'wp-admin/includes/file.php\';
require_once ABSPATH . \'wp-admin/includes/media.php\';
require_once ABSPATH . \'wp-admin/includes/image.php\';
$temp = download_url( $url );
if ( ! is_wp_error( $temp ) && $info = @ getimagesize( $temp ) ) {
if ( ! strlen( $title ) )
$title = null;
if ( ! $ext = image_type_to_extension( $info[2] ) )
$ext = \'.jpg\';
$data = array(
\'name\' => md5( $url ) . $ext,
\'tmp_name\' => $temp,
);
$id = media_handle_sideload( $data, $this->_post_id, $title );
if ( ! is_wp_error( $id ) )
return update_post_meta( $this->_post_id, \'_thumbnail_id\', $this->_thumb_id = $id );
}
if ( ! is_wp_error( $temp ) )
@ unlink( $temp );
}
}
我尝试修改此部分(从正上方):
if ( $content = get_post_field( \'film_embed\', $this->_post_id, \'raw\' ) ) {
add_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
$wp_embed->autoembed( $content );
remove_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
}
。。。这样就包括了
_cmb_
前缀为
film_embed
:
if ( $content = get_post_field( \'film_embed\', $this->_post_id, \'raw\' ) || $content = get_post_field( \'_cmb_film_embed\', $this->_post_id, \'raw\' ) ) {
add_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
$wp_embed->autoembed( $content );
remove_filter( \'oembed_dataparse\', array( $this, \'oembed_dataparse\' ), 10, 3 );
}
。。。但它没有起作用。我错过了什么?