自定义元盒ID前缀显示在第一个类别上,但不显示在其他类别上

时间:2014-09-18 作者:dmoz

我已经创建了一个自定义帖子类型,并使用这一伟大的资源添加了一些自定义元框: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 );

}
。。。但它没有起作用。我错过了什么?

1 个回复
SO网友:Steve Mitchell

据我所知,这源于你使用get_post_field(), 这是为数据库posts表中的字段保留的(即:内置到WordPress,而不是自定义字段)。对于应使用的自定义字段get_post_meta().

结束

相关推荐

YouTube oEmbedded和隐私增强模式

在iframe中嵌入youtube视频时,可以启用隐私增强模式,这样youtube在播放视频之前不会存储有关网页访问者的信息。我尝试通过oEmbed和URL嵌入视频http://www.youtube-nocookie.com/embed/xA3tfBTvH0c但它没有起作用。是否有机会使用oEmbed实现隐私友好的解决方案?编辑我找到的this proposal 并试图对其进行定制,但有一点并不是最优的。您不能使用定义的$content\\u width,因为此解决方案也需要声明高度。对这种方法有什么想