页面元框-Get_Categories下拉列表

时间:2012-01-16 作者:flowdee

我创建了一个包含所有现有类别的下拉列表的元框。

页面管理员应该能够从下拉列表中选择一个类别,该类别将由wordpress的保存/更新页面功能保存。

<select name="event-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> 
 <option value=""><?php echo esc_attr(__(\'Select Event\')); ?></option> 
 <?php 
  $categories=  get_categories(\'child_of=10\'); 
  foreach ($categories as $category) {
    $option = \'<option value="/category/archives/\'.$category->category_nicename.\'">\';
    $option .= $category->cat_name;
    $option .= \' (\'.$category->category_count.\')\';
    $option .= \'</option>\';
    echo $option;
  }
 ?>
</select>
我完全不知道如何在不重定向到其他页面(如“action=bla.php”)的情况下使用所选列表项?!

在过去,我使用简单的输入字段,这些字段可以通过更新/保存页面功能保存,但是如何使用下拉列表来保存呢?

以下是完整的代码:

<?php
/*
Plugin Name: myname &raquo; Membership
Plugin URI: http://www.myname.com
Description: Das membership Plugin.
Author: myname
Version: 2
Author URI: http://www.myname.com
*/

add_action( \'add_meta_boxes\', \'membership_meta_box_add\' );

function membership_meta_box_add() {
/* 
 * add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
 * id = unique identifier for css or javascript
 * title = displayed at the top of the meta box
 * callback = function which renders the meta box
 * page = will be displayed on "post", "page" or "some_custom_post_type"
 * context = where to place the box: "normal" (below the editor), "side" and "advanced" (column of post editor but further down)
 * priority = from top to bottom: "high", "default", "low"
 * callback_args = will the disregard by us
 */

    add_meta_box( \'myname_membership_box_id\', \'myname Membership Einstellungen\', \'membership_meta_box_cb\', \'page\', \'normal\', \'high\' ); 
}

function membership_meta_box_cb($post) {  

    // $post is already set, and contains an object: the WordPress post
    $values = get_post_custom( $post->ID );

    /***************************************************
     *              Einlesen der bereits gespeicherten Variablen
     ***************************************************/

    $membership_premium_category = isset( $values[\'membership_premium_category\'] ) ? esc_attr($values[\'membership_premium_category\'][0]) : \'\';  

    // We\'ll use this nonce field later on when saving.
    wp_nonce_field( \'membership_meta_box_ms_nonce\', \'meta_box_ms_nonce\' );

    /***************************************************
     *              Konfiguration der Stylesheets
     ***************************************************/   
?>
    <style type="text/css">
        .membership-box-content {padding: 10px 0px 0px 0px; margin: 10px 0px 0px 0px; clear: both;}
        .membership-box-content-switcher {clear: both;}
        .membership-box-title {margin: 0px 0px 5px 0px; font-weight: bold; float: left;}
        .membership-box-dev-note {font-style: italic; float: right; margin-right: 10px;}
        .membership-box-field {margin: 0px 0px 5px 0px; width: 99%;}
        .membership-box-field:hover {background: #F9F9F9;}
        .membership-box-field:focus {background: #F9F9F9;}
        .membership-box-field-small:hover {background: #F9F9F9;}
        .membership-box-field-small:focus {background: #F9F9F9;}
        .membership-box-details {margin: 0px 0px 10px 0px;}
        .extend-button {
            margin: 5px 0px 0px 0px;
            -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
            -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
            box-shadow:inset 0px 1px 0px 0px #ffffff;
            background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
            background:-moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
            filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=\'#ededed\', endColorstr=\'#dfdfdf\');
            background-color:#ededed;
            -moz-border-radius:5px;
            -webkit-border-radius:5px;
            border-radius:5px;
            border:1px solid #dcdcdc;
            display:inline-block;
            color:#777777;
            font-family:Verdana;
            font-size:10px;
            font-weight:normal;
            padding:3px 8px;
            text-decoration:none;
            text-shadow:1px 1px 0px #ffffff;
        }
        .extend-button:hover {
            background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #dfdfdf), color-stop(1, #ededed) );
            background:-moz-linear-gradient( center top, #dfdfdf 5%, #ededed 100% );
            filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=\'#dfdfdf\', endColorstr=\'#ededed\');
            background-color:#dfdfdf;
        }
        .extend-button:active {
            position:relative;
            top:1px;
        }
    </style>

    <?
    /***************************************************
     *              Aufbau der Eingabefelder
     ***************************************************/   
    ?>

    <p>Hier finden Sie alle verf&uuml;gbaren Einstellungsm&ouml;glichkeiten f&uuml;r die Membership Site.</p>

    <div id="inhaltselemente">
        <div class="membership-box-content">
            <div class="membership-box-title">Kategorie f&uuml;r Membership Artikel</div><div class="membership-box-dev-note">dev: "membership_premium_category"</div>
            <input type="text" class="membership-box-field" name="membership_premium_category" id="membership_premium_category" value="<?php echo $membership_premium_category; ?>" />
            <div class="membership-box-details">Bitte tragen Sie hier den Namen (nicht Slug/Titelform) der obersten Kategorie aller Premium Artikel ein. <i>Diese Eingabe ist notwendig.</i></div>
        </div>
    </div>

    <!-- WORKING DRAFT -->
    <select name="event-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> 
        <option value=""><?php echo esc_attr(__(\'Select Event\')); ?></option> 
        <?php 
        $categories=  get_categories(\'child_of=10\'); 
        foreach ($categories as $category) {
            $option = \'<option value="/category/archives/\'.$category->category_nicename.\'">\';
            $option .= $category->cat_name;
            $option .= \' (\'.$category->category_count.\')\';
            $option .= \'</option>\';
            echo $option;
        }
        ?>
    </select>
    <!-- WORKING DRAFT -->

<?php
} 

    /***************************************************
     *              Speicherfunktion des Plugins
     ***************************************************/

add_action( \'save_post\', \'membership_meta_box_save\' );

function membership_meta_box_save( $post_id )  
{  
    // Bail if we\'re doing an auto save  
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn\'t there, or we can\'t verify it, bail 
    if( !isset( $_POST[\'meta_box_ms_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_ms_nonce\'], \'membership_meta_box_ms_nonce\' ) ) return; 

    // if our current user can\'t edit this post, bail  
    if( !current_user_can( \'edit_post\' ) ) return;  

    // now we can actually save the data  
    $allowed = array(\'a\' => array( \'href\' => array()),
                            \'object\' => \'\',
                            \'iframe\' => array( \'width\' => array(), \'height\' => array(), \'src\' => array(), \'frameborder\' => array(), \'allowfullscreen\' => array())  
    );  

    /***************************************************
     *              Speicherung der einzelnen oben definierten Felder
     ***************************************************/

    // Make sure your data is set before trying to save it
    if( isset( $_POST[\'membership_premium_category\'] ) )
        update_post_meta( $post_id, \'membership_premium_category\', wp_kses( $_POST[\'membership_premium_category\'] ) );
} 
?>

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

如果您在元数据库中使用它,那么您真的不需要此部分:

onchange=\'document.location.href=this.options[this.selectedIndex].value;\'
因此,请将“选择”字段更改为:

<select name="event-dropdown"> 
    <option value=""><?php echo esc_attr(__(\'Select Event\')); ?></option> 
    <?php 
    //get saved data
    $saved_cat = get_post_meta($post_id,\'event-dropdown\',true);
    $categories=  get_categories(\'child_of=10\'); 
    $select_options = \'\';
    foreach ($categories as $category) {
        $option = \'<option value="\'.$category->cat_ID.\'">\';
        $option .= $category->cat_name;
        $option .= \'</option>\';
        $select_options .= $option;
    }
    //set saved data as selected
    $select_options = str_replace(\'value="\'.$saved_cat.\'"\',\'value="\'.$saved_cat.\'" selected="selected"\',$select_options);
    echo $select_options;
    ?>
</select>
然后确保将其保存在membership_meta_box_save 函数的末尾添加以下内容:

if( isset( $_POST[\'event-dropdown\'] ) )
        update_post_meta( $post_id, \'event-dropdown\', $_POST[\'event-dropdown\'] );
现在,类别id将保存为每个页面的元

结束

相关推荐

如何重置此wp_list_categories查询?

如何重置生成具有wp\\U list\\U类别的类别列表的查询?下面的查询构建了一个两列的类别列表,然后与第二个代码块一起显示。我在jQuery选项卡中使用其中两个查询(具有不同的类别包含字符串)来显示不同的类别列表。问题是,第一个查询需要重置,因为第二个选项卡显示两个类别列表,而不是仅显示第二个查询中的类别。我试过了<?php wp_reset_query();?> 但这只适用于WP循环。有人有什么想法吗? <?php $cats = explode