2

我的自定义帖子类型具有自定义元框,其中包含input字段和wp_editor,在代码下方:

<div class="inside">
    <div>
        <label>Title</label>
        <input type="text" name="title[]">
    </div>
    <div>
        <label>Type</label>
        <input type="text" name="type[]">
    </div>
    <div>
        <label>Content</label>
        '.wp_editor($content, 'text', array(
        'wpautop' => true,
        'media_buttons' => false,
        'textarea_rows' => 5
        )
        ).'
    </div>
</div>

wp_editor位于顶部和.insidediv 之外。

如何在 Content 标签下方的 div 中包含wp_editor.inside

4

2 回答 2

2

使用 ob_start() 和 ob_get_contents()。

您通常将其与其他两个函数配对:ob_get_contents(),它基本上为您提供自从使用 ob_start() 打开缓冲区以来“保存”到缓冲区的任何内容,然后是 ob_end_clean() 或 ob_flush()

附加输出。

在此处输入图像描述

代码。

 ob_start();
         wp_editor($content, 'text', array(
            'wpautop' => true,
            'media_buttons' => false,
            'textarea_rows' => 5
            )
            );
         $output = ob_get_clean();

        echo '
        <div class="inside">
    <div>
        <label>Title</label>
        <input type="text" name="title[]">
    </div>
    <div>
        <label>Type</label>
        <input type="text" name="type[]">
    </div>
   <div style="clear:both"></div>
    <div>
        <label>Content</label>
        '.$output.'
    </div>
</div>';
于 2019-06-17T07:11:17.287 回答
0
add_action( 'add_meta_boxes', 'global_banner_setting_meta_box' );
function global_banner_setting_meta_box() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

    add_meta_box(
        'banner_setting_data',
        __( 'Banner Setting', 'launchpm' ),
        'render_metabox',
        'page',
        'advanced',
        'default'
    );
}
function render_metabox( $post ) {

    field_generator( $post );
}    
function field_generator( $post ) {
    wp_nonce_field( 'banner_nonce_action', 'banner_nonce' );

    $banner_title_data = get_post_meta($post->ID,'banner_title_data_',true);
    $banner_description_data = get_post_meta($post->ID,'banner_description_data_',true);

    if(empty($banner_title_data)){$banner_title_data = '';}
    if(empty($banner_description_data)){$banner_description_data = '';}

    $banner_html_code = '<div id="postcustomstuff">';
    $banner_html_code .='<table class="form-table">';
    $banner_html_code .='<tr>';
    $banner_html_code .='<td><label style="display:block; font-weight:600; margin:0 8px;" for="banner_title_data" class="banner_title_label">' . __( 'Banner Title', 'launchpm' ) . '</label><input type="text" id="banner_title_data" name="banner_title_data" class="banner_title_data_field" placeholder="' . esc_attr__( '', 'launchpm' ) . '" value="' . esc_attr__( $banner_title_data ) . '"></td>';
    $banner_html_code .='</tr>';
    $banner_html_code .='<tr>';
    $banner_html_code .='<td><label style="display:block; font-weight:600; margin:0 8px;" for="banner_description_data" class=banner_description_data_label">' . __( 'Banner Description', 'launchpm' ) . '</label>
    <textarea id="banner_description_data" class="textarea banner_description_data_field" name="banner_description_data" placeholder="' . esc_attr__( '', 'launchpm' ) . '" rows="8">' . esc_attr__( $banner_description_data ) . '</textarea></td>
    ';
    $banner_html_code .='</tr>';
    $banner_html_code .='</table>';
    $banner_html_code .='</div>';
    echo $banner_html_code;
}
add_action( 'save_post','save_metabox_banner_data', 10, 2);
function save_metabox_banner_data($post_id, $post)
{
    $nonce_name   = $_POST['banner_nonce'];
    $nonce_action = 'banner_nonce_action';

    if ( ! isset( $nonce_name ) )
        return;
    if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
        return;
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;
    if ( wp_is_post_autosave( $post_id ) )
        return;
    if ( wp_is_post_revision( $post_id ) )
        return;

    $banner_title_data = isset( $_POST[ 'banner_title_data' ] ) ?  $_POST[ 'banner_title_data' ]  : '';
    $banner_description_data = isset( $_POST[ 'banner_description_data' ] ) ? $_POST[ 'banner_description_data' ] : '';

    // Update the meta field in the database.
    update_post_meta( $post_id, 'banner_title_data_', $banner_title_data );
    update_post_meta( $post_id, 'banner_description_data_', $banner_description_data );

}
于 2019-06-17T05:46:52.133 回答