drf65218 2012-12-03 10:59
浏览 59
已采纳

短代码在自定义元框中不起作用

I am having issue with WordPress meta boxes. actually I am using WordPress Genesis framework and in child theme I had create few meta boxes for my client to show some content before the page content, but in the custom meta box i am using wp-editor and its working fine. but the issue is that when I try to use some shortcodes in this wp-editor then it won't show me anything, it is just returning the whole shortcode as it is.

I am using https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress for custom meta boxes.

And my codes are in function.php file:

/* -------------------------------------------------------------------------- */
/* Setup Custom metaboxes                                                     */
/* -------------------------------------------------------------------------- */
add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );

function be_initialize_cmb_meta_boxes() {
    if ( !class_exists( 'cmb_Meta_Box' ) ) {
        require_once( CHILD_DIR . '/lib/metabox/init.php' );
    }
}

add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );

function cmb_sample_metaboxes( array $meta_boxes ) {

    // Start with an underscore to hide fields from custom fields list
    $prefix = '_cmb_';

    $meta_boxes[] = array(
        'id'         => 'text_content',
        'title'      => 'Text Content',
        'pages'      => array( 'page', ), // Post type
        'context'    => 'normal',
        'priority'   => 'high',
        'show_names' => true, // Show field names on the left
        'fields'     => array(
            array(
                'name' => 'Custom Content',
                'desc' => 'This is a title description',
                'id'   => $prefix . 'custom_content',
                'type' => 'title',
            ),  
            array(
                'name' => 'Tab Name',
                'desc' => 'Please descibe the tab name (required)',
                'id'   => $prefix . 'tab_name',
                'type' => 'text',
            ),
            array(
                'name'    => 'Test wysiwyg',
                'desc'    => 'field description (optional)',
                'id'      => $prefix . 'test_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            ),
        ),
    );

    return $meta_boxes;
}

I save the codes in page.php as:

add_action('genesis_before_loop', 'ehline_before_loop_content');

function ehline_before_loop_content() 
{
    echo genesis_get_custom_field( '_cmb_tab_name' );
    echo '<br />';
    echo genesis_get_custom_field( '_cmb_test_wysiwyg' );
}
genesis();

But when I use the shortcodes in this meta box it return something like that

[wptabtitle] Tab 01[/wptabtitle] [wptabcontent]test[/wptabcontent]

Please anyone tell me how can I make it to use shortcodes in the wp-editor.

  • 写回答

1条回答 默认 最新

  • douhui9192 2012-12-03 12:01
    关注

    You need to call do_shortcode() for the content of your custom fields. Here is how the updated code should look like:

    add_action('genesis_before_loop', 'ehline_before_loop_content');
    
    function ehline_before_loop_content() 
    {
        echo do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) );
        echo '<br />';
        echo do_shortcode( genesis_get_custom_field( '_cmb_test_wysiwyg' ) );
    }
    genesis();
    

    Also this will not add the auto-paragraphs that you would usually see for your posts contents. You can do two things:

    echo apply_filters( 'the_content', genesis_get_custom_field( '_cmb_tab_name' ) );
    

    or

    echo wpautop( do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) ) );
    

    In theory the first one should be better, but sometimes you might get additional output from functions that hook to the the_content filter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器