web-dev-qa-db-ja.com

タイトルの後にポスト編集画面でmeta_boxを配置するにはどうすればよいですか。

add_meta_box()を使ってEdit Post画面にdivを表示しています。この機能は "priority"と "context"パラメータを介して非常に限られたポジショニングオプションを提供し、これらは私のニーズには十分ではありません。

私はdivをパーマリンクの下、ただしEdit PostのInsert Mediaボタンの上、特にdiv "titleiv"の下、div "postdivrich"の上に表示できる必要があります。

ポスト編集画面にメタボックスを配置する方法は他にありますか。これはjQueryを介して行うことができますか?

これは部分的にはたらく解決策です:

function admin_init(){
  add_meta_box("wd_meta", "WD Meta", "wd_meta", "post", "normal", "high");
}
add_action("admin_init", "admin_init");

function wd_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
?>
<div id="wdMeta">
<p>Display a message here.</p>
</div> 

<script>
$('#wd_meta').insertAfter('#titlediv'); 
</script>
<?php } ?>

メタボックスはページ上に正しく表示されていますが、デモでは機能するように見えますが、JavaScriptによって正しく配置されていません。

http://jsfiddle.net/kFTc5/11/

6
AMcDermott

実際のメタボックスを使用してそれを行うことはできません。代わりにedit_form_after_titleにフックしてください。

enter image description here

これは簡単な例です:

add_action( 'edit_form_after_title', 'wpse_87478_pseudo_metabox' );
add_action( 'save_post', 'wpse_87478_save_metabox' );

function wpse_87478_pseudo_metabox()
{
    global $post;
    $key = '_wpse_87478';

    if ( empty ( $post ) || 'post' !== get_post_type( $GLOBALS['post'] ) )
        return;

    if ( ! $content = get_post_meta( $post->ID, $key, TRUE ) )
        $content = '';

    printf(
        '<p><label for="%1$s_id">Enter some text
        <input type="text" name="%1$s" id="%1$s_id" value="%2$s" class="all-options" />
        </label></p>',
        $key,
        esc_attr( $content )
    );
}

function wpse_87478_save_metabox( $post_id )
{
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    $key = '_wpse_87478';

    if ( isset ( $_POST[ $key ] ) )
        return update_post_meta( $post_id, $key, $_POST[ $key ] );

    delete_post_meta( $post_id, $key );
}
9
fuxia