1

将以下脚本添加到 Wordpress 中的 function.php 文件中:

add_action( 'add_meta_boxes', 'rating_select_box' );
function rating_select_box() {
    add_meta_box(
        'rating_select_box', // id, used as the html id att
        __( 'Select rating:' ), // meta box title, like "Page Attributes"
        'rating_select_cb', // callback function, spits out the content
        'post', // post type or page. We'll add this to posts only
        'side', // context (where on the screen
        'low' // priority, where should this go in the context?
    );

}

function rating_select_cb( $post )
{
    global $wpdb;
    $value = get_post_meta($post->ID, 'rating', true);
    echo '<div class="misc-pub-section misc-pub-section-last">
         <span id="timestamp">'
         . '<label>Select a rating:<br></label>';

         $selected = ($value == $result->post_name) ? ' selected="selected" ' : null;

         echo '<select name="rating">';
         echo '<option value="" default="default"> None... </option>';
         echo '<option value="0" '.$selected.'> G — Suitable for all audiences </option>';
         echo '<option value="1" '.$selected.'> PG — Possibly offensive, usually for audiences 13 and above </option>';
         echo '<option value="2" '.$selected.'> R — Intended for adult audiences above 17 </option>';
         echo '<option value="3" '.$selected.'> X — Even more mature than above </option>';
         echo '</select>';      

    echo '</span></div>';
}

add_action( 'save_post', 'save_metadata');

function save_metadata($postid)
{   
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $postid ) ) return false;
    if( empty($postid) ) return false;


    if ( is_null($_REQUEST["rating"]) ) {
        delete_post_meta($postid, 'rating');
    } else {
        update_post_meta($postid, 'rating', $_REQUEST['rating']);
    }

}

将在帖子页面中添加一个带有下拉列表的框。我希望启用默认选项,None...因此我default="default"在该选项中添加了一个,但它不起作用。默认情况下,选择最大的数值,在这种情况下,它是X — Even more mature than above

我在做什么错?我怎样才能使它None...默认选择该选项?

4

2 回答 2

3

select列表中,“选择”选项的正确方法是使用selected="selected",而不是default="default"。将您的“无”选项更改为以下内容应该可以解决它:

echo '<option value="" selected="selected"> None... </option>';

但是,您不是在动态创建select列表,您只是在输出一个完整列表,并且您也会遇到选择其他值的问题。要解决这个问题,如果选项不会改变,您可以将它们放在一个数组中以循环它们。您可以使用以下新代码修改您的rating_select_cb()方法以实现此目的:

function rating_select_cb( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, 'rating', true);
    echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Select a rating:<br></label>';

    // build an array of each available rating
    $ratings = array(
        1 => 'G — Suitable for all audiences',
        2 => 'PG — Possibly offensive, usually for audiences 13 and above',
        3 => 'R — Intended for adult audiences above 17',
        4 => 'X — Even more mature than above'
    );

    echo '<select name="rating">';
    echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </option>';

    // output each rating as an option
    foreach ($ratings as $id => $text) {
        echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
    }
    echo '</select>';

    echo '</span></div>';
}

如果选择的选项不在可用的评级列表中,此方法将默认选择“无”选项。$value之后,它将遍历每个评级并将其作为选项输出。如果评级$id与 selected 匹配$value,它将被标记为选中。

于 2012-11-05T14:15:57.083 回答
2

你应该像这样使用它:

echo '<option value="" selected="selected"> None... </option>';
于 2012-11-05T14:07:54.450 回答