2

WordPress メタボックスに問題があります。実際、私は WordPress Genesis フレームワークを使用しており、子テーマではクライアントがページ コンテンツの前にいくつかのコンテンツを表示するためのメタ ボックスをいくつか作成していましたが、カスタム メタ ボックスでは wp-editor を使用しており、正常に動作しています。しかし、問題は、この wp-editor でいくつかのショートコードを使用しようとすると、何も表示されず、ショートコード全体がそのまま返されることです。

カスタム メタ ボックスに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 でショートコードを使用する方法を教えてください。

4

1 に答える 1

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

また、これは、投稿のコンテンツに通常表示される自動段落を追加しません。次の 2 つのことを行うことができます。

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フィルターにフックする関数から追加の出力が得られる場合があります。

于 2012-12-03T12:01:16.263 に答える