在自定义帖子类型中添加自定义帖子格式选项

时间:2015-05-08 作者:Siddharth Thevaril

我使用以下代码添加了自定义的帖子格式选项-

function wpb_mce_buttons_2($buttons) {
    array_unshift($buttons, \'styleselect\');
    return $buttons;
}
add_filter(\'mce_buttons_2\', \'wpb_mce_buttons_2\');

 function my_mce_before_init_insert_formats( $init_array ) 
 {

// Define the style_formats array

    $style_formats = array(
        // Each array child is a format with it\'s own settings
        array(
            \'title\' => \'Post Title Block\',
            \'block\' => \'div\',
            \'classes\' => \'post-title-block\',
            \'wrapper\' => true,

        ),
        array(
            \'title\' => \'Post Content Block\',
            \'block\' => \'div\',
            \'classes\' => \'post-content-block\',
            \'wrapper\' => true,
        ),

        array(
            \'title\' => \'Important\',
            \'block\' => \'div\',
            \'classes\' => \'important-content-block\',
            \'wrapper\' => true,
        ),
        array(
            \'title\' => \'Image center\',
            \'block\' => \'div\',
            \'classes\' => \'img-center-post\',
            \'wrapper\' => true,
        ),
                array(
                        \'title\' => \'Program number\',
                        \'block\' => \'div\',
                        \'classes\' => \'program-num\',
                        \'wrapper\' => true,
                ),
    );
    // Insert the array, JSON ENCODED, into \'style_formats\'
    $init_array[\'style_formats\'] = json_encode( $style_formats );

    return $init_array;
}
// Attach callback to \'tiny_mce_before_init\'
add_filter( \'tiny_mce_before_init\', \'my_mce_before_init_insert_formats\' );
我有2个custom post types. 我希望自定义帖子格式选项只显示在自定义帖子类型中的一种。我该怎么做?

我试过使用if ( !(get_post_type() == \'mech_tuts\') ) 内部

function my_mce_before_init_insert_formats( $init_array ) 
定义,其中mech_tuts 是post类型,但会产生意外结果。我如何继续?

1 个回复
SO网友:Hannes

也许为时已晚,但我发现了如何解决这个问题,所以我想我可以分享我的解决方案。您必须访问全局$post变量才能找到post类型:

function haet_custom_toolbar( $initArray ) {  
    global $post;
    $post_type = get_post_type( $post->ID );

    if( \'page\' == $post_type ){
        $initArray[\'toolbar1\'] = \'formatselect,styleselect,|,bold,italic,|,alignleft,aligncenter,alignright,|,pastetext,removeformat,|,undo,redo,|,bullist,numlist,|,link,unlink,|,spellchecker,fullscreen\';
        $initArray[\'toolbar2\'] = \'\';    
    }else if( \'product\' == $post_type ){
        $initArray[\'toolbar1\'] = \'pastetext,removeformat,|,undo,redo,|,link,unlink,|,code\';
        $initArray[\'toolbar2\'] = \'\';    
    }

    return $initArray;  
} 
add_filter( \'tiny_mce_before_init\', \'haet_custom_toolbar\' );  
下面是一篇关于它的博客文章,其中包含WordPress编辑器的所有可用按钮:Customize WordPress Editor for WooCommerce Products

结束