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

时间:2012-12-03 10:59:08

标签: php wordpress shortcode

我遇到了WordPress元框的问题。实际上我正在使用WordPress Genesis框架,在子主题中我创建了几个元框供我的客户端在页面内容之前显示一些内容,但在自定义元框中我使用wp-editor及其工作正常。但问题是,当我尝试在这个wp编辑器中使用一些短代码时,它不会向我显示任何内容,它只是返回整个短代码。

我正在使用https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress自定义元框。

我的代码位于 function.php 文件中:

/* -------------------------------------------------------------------------- */
/* 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;
}

我将代码保存在 page.php 中:

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();

但是当我在这个元框中使用短代码时,它会返回类似

的内容
[wptabtitle] Tab 01[/wptabtitle] [wptabcontent]test[/wptabcontent]

请有人告诉我如何在wp-editor中使用短代码。

1 个答案:

答案 0 :(得分:1)

您需要致电do_shortcode()了解自定义字段的内容。以下是更新代码的外观:

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();

此外,这不会添加您通常会在帖子内容中看到的自动段落。你可以做两件事:

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

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

理论上,第一个应该更好,但有时您可能会从挂钩到the_content过滤器的函数中获得额外的输出。

相关问题