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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀