問題描述

我希望使用者能夠根據需要建立和刪除其他元框欄位。

例如,說每個劇集播放的歌曲數量不一樣的音樂播客。使用者應該可以點選一個按鈕,新增其他欄位,以便根據需要輸入每首歌曲。

理想情況下,這樣做不需要使用外掛,而是編碼到函式檔案中。

最佳解決方案

所以你的意思是這樣的?

當您點選新增軌道時,將成為:

如果這是透過建立一個具有簡單 jquery 函式來新增和刪除其中的欄位的 metabox,並將資料作為陣列儲存在單個元行中的資料中,那麼您可以:

add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
    add_meta_box(
        'dynamic_sectionid',
        __( 'My Tracks', 'myplugin_textdomain' ),
        'dynamic_inner_custom_box',
        'post');
}

/* Prints the box content */
function dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $songs = get_post_meta($post->ID,'songs',true);

    $c = 0;
    if ( count( $songs ) > 0 ) {
        foreach( $songs as $track ) {
            if ( isset( $track['title'] ) || isset( $track['track'] ) ) {
                printf( '<p>Song Title <input type="text" name="songs[%1$s][title]" value="%2$s" /> -- Track number : <input type="text" name="songs[%1$s][track]" value="%3$s" /><span class="remove">%4$s</span></p>', $c, $track['title'], $track['track'], __( 'Remove Track' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="here"></span>
<span class="add"><?php _e('Add Tracks'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;

            $('#here').append('<p> Song Title <input type="text" name="songs['+count+'][title]" value="" /> -- Track number : <input type="text" name="songs['+count+'][track]" value="" /><span class="remove">Remove Track</span></p>' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine.
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    // OK, we're authenticated: we need to find and save the data

    $songs = $_POST['songs'];

    update_post_meta($post_id,'songs',$songs);
}

次佳解決方案

這是透過自定義欄位完成的,但是您不應該使用任何允許使用者新增建立或刪除元框的內容。這些直接寫入資料庫,因此如果您向使用者提供這種控制,您可能會為您的網站造成很多問題。為您建立可能需要的最大數量的自定義欄位遠遠更安全,並讓他們在不需要的地方留下空白。

這也是外掛領域。功能檔案是 theme-specific,而外掛適用於適用於網站內容的功能,特別是如果您希望該內容可用,無論使用哪種主題。

一些建議:

 http://wordpress.org/extend/plugins/verve-meta-boxes/

 http://wordpress.org/extend/plugins/more-fields/

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。