質問

私の目標はシンプルでした---タグやチェックボックスを使用するのではなく、分類項の単純なドロップダウンリストを備えたカスタムメタボックスを備えたカスタム投稿タイプを作成することでした。その理由は、編集者が分類リストから単一の用語のみを選択できるようにすることでした。

試行錯誤の後、私は最終的にWP-アルケミー(http://farinspace.com/wpalchemy-metabox/)から使用している優れたメタボックス作成ツールを利用する方法を見つけました。ドロップダウンリストの用語。

私が抱えている問題は、選択した値を保存するためにドロップダウンメニューから新しい選択を取得できないように見えることです。

明確にするために...このカスタム投稿タイプの各投稿に単一の用語のみが関連付けられることは重要です。つまり、ドロップダウンメニューから異なる値が選択され、投稿が保存されると、以前の選択が未登録のものであることを確認する必要があります。

前述のように、私は現在、ドロップダウンに用語のリストを表示していることがあり、関連する可能性のある用語を正しく表示しています。

新しい値を節約するだけで、私が問題を抱えている場所です。

広範な研究から、ソリューションのように「WordPress」を使用することが含まれているように思われます。http://codex.wordpress.org/function_reference/wp_set_post_termsしかし、私の希望する結果を達成するためにそれを正しく利用する方法がわかりません。

以下に、利用しているコードを含めています。これを行うためのより良い方法がある可能性が非常に高く、皆さんが持っている可能性のある提案は、承認されていないユーザーがデータを挿入できないことを確認するために、該当する「チェック」が行われていることを保証する可能性があります。

functions.phpファイル(以下のカスタムポストタイプと分類法の作成を示しています。

//////////////////////////////////////////////////////////////////////////////
// CUSTOM POSTTYPE FOR -- SERVICES  
//////////////////////////////////////////////////////////////////////////////

add_action('init', 'services');
function services() {
 register_post_type('services', array(
  'labels' => array(
   'name'   => __('Services'),
   'singular_label'  => __('Service'),
   'new_item'   => __('Add a Service'),
   'add_new'   => __('Add a Service'),
   'add_new_item'  => __('Add a Service'),
   'edit'   => __('Edit Service'),
   'edit_item'   => __('Edit Service'),
   'view'   => __('View Service'),
   'view_item'   => __('View Service'),
   'search_items'  => __('Search Services'),
   'not_found'   => __('No Services Found'),
   'not_found_in_trash' => __('No Services Found in Trash'),
       'parent_item'  => __('Parent Service'),
   'parent_item_colon' => __('Parent Service:')
   ),
  'can_export'    => true,
  'menu_position'   => 7,
  'public'    => false,
  'show_ui'    => true,
  'publicly_queryable'  => true,
  'hierarchical'   => true,
  'query_var'    => true,
  'capability_type'   => 'post',
  'exclude_from_search'  => false,
  'supports' => array(
   'title',
   'editor',
   'revisions',
   'page-attributes'
   ),
  'rewrite' => array( 
   'slug'   => 'disability-services', 
   'with_front'   => false
   )
 ));
}

これが私が一緒に仕事をしようとしている分類法を登録しているところです

// CUSTOM TAXONOMY METABOX POSTTYPE - AGE GROUPS
   register_taxonomy('theme', array('services'), array(
    'hierarchical'          => false, 
    'singular_label'            => 'Age Group', 
    'query_var'                 => 'theme',
    'public'                => true,
    'show_ui'               => true,
    'show_tagcloud'             => false,
    'show_in_nav_menus'             => true,    
    'rewrite'               => array( 'slug' => 'age-groups', 'with_front' => false ),
    'labels' => array(
        'name'          => __( 'Age Groups' ),
        'singular_name'         => __( 'Age Groups' ),
        'search_items'      => __( 'Search Age Groups' ),
        'all_items'             => __( 'All Age Groups' ),
        'parent_item'       => __( 'Parent Age Group' ),
        'parent_item_colon'         => __( 'Parent Age Group:' ),
        'edit_item'             => __( 'Edit Age Group' ), 
        'update_item'       => __( 'Update Age Group' ),
        'add_new_item'      => __( 'Add Age Group' ),
        'new_item_name'         => __( 'New Name of Age Group' ),
        'popular_items'         => __( 'Popular Age Groups' ),
        'separate_items_with_commas'=> __( 'Separate Age Groups with commas' ),
        'add_or_remove_items'   => __( 'Add or remove Age Groups' ),
        'choose_from_most_used' => __( 'Select Popular Age Groups' ), 
        ),
   ));
   wp_insert_term('Kids', 'theme');
   wp_insert_term('Teens', 'theme');
   wp_insert_term('Adults', 'theme');

これは、機能ファイルとコードで使用しているコードの残りの部分であり、WPalechemyに基づいてメタボックスを作成します。この試みでは、「save_filter」=> "wp_set_post_terms($ post-> id、 'thema')を含めようとしました。

// CUSTOM METABOX POSTTYPE - SERVICE DETAILS
   $custom_metabox_service_details = new WPAlchemy_MetaBox(array (
    'id'        => '_service_details-metaboxes',        // underscore prefix hides fields from the custom fields area
    'title'     => 'Age Groups',            // title added automatically to the custom metabox
    'types'     => array('services'),           // added only for custom post type "name-of-post-type" can also be "page" or "post"
    'context'   => 'normal',                    // same as above, defaults to "normal" but can use "advanced" or "side"
    'priority'  => 'high',                  // same as above, defaults to "high" but can use "low" as well
    'mode'  => WPALCHEMY_MODE_EXTRACT,
    'save_filter' => "wp_set_post_terms( $post->ID, 'theme' )",
    'template'  => TEMPLATEPATH . '/admin-metabox/service_details-metaboxes.php'  // contents for the meta box
    ));

また、WPalechmeyのDimasが彼の1.3.2バージョンのGitHubにいくつかの新しいコードを追加しただけで、上記のコードがを含めることができることに注意する必要があります。

'save_filter' => "custom-function", 

このコードを使用すると、カスタム関数を作成できます。または、パブリッシュボタンを押すと実行されるカスタム関数を呼び出すと思います。

いずれにせよ、カスタムメタボックスに次のコードを利用して、分類項を表示する実際のドロップダウンリストを表示しています。

<?php 
// This function gets called in edit-form-advanced.php
echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' . wp_create_nonce( 'taxonomy_theme' ) . '" />';

// Get all theme taxonomy terms
$themes = get_terms('theme', 'hide_empty=0'); 
?>

<select name='post_theme' id='post_theme'>
// DISPLAY TERMS AS DROP DOWN OPTIONS
<?php $names = wp_get_object_terms($post->ID, 'theme'); ?>
<option class='theme-option' value='' <?php if (!count($names)) echo "selected";?>>None</option>
<?php foreach ($themes as $theme) {
 if (!is_wp_error($names) && !empty($names) && !strcmp($theme->slug, $names[0]->slug)) 
 echo "<option class='theme-option' value='" . $theme->slug . "' selected>" . $theme->name . "</option>\n"; 
 else
 echo "<option class='theme-option' value='" . $theme->slug . "'>" . $theme->name . "</option>\n"; 
}
?>
</select>

データを保存することはパイのように簡単であるべきだと思っていますが、これを達成する方法について自分自身を混乱させていると思います。

前述のように、データが正しく保存され、正しく認可された人々のみであることを確認するために必要なチェックが行われるようにコードに関する提案を提供できれば感謝します。

よろしくお願いします!

役に立ちましたか?

解決

興味のある人のために、私は別の投稿でこの質問に答えました:

分類条件の節約

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top