添加下面的腳本到function.php文件中的WordPress:默認爲「默認」的選項不起作用
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...
選項?
您能否詳細說明我將在哪裏添加此代碼示例?如果您可以友好地將其與我的原始代碼的其餘部分一起發佈... –
感謝您的更新,請允許我幾分鐘測試此代碼並查看它的功能。將很快回報 –
我剛剛測試過,這裏是結果。默認情況下,選擇值爲0,即** G - 適用於所有受衆**。也許在你的代碼中有一個錯字?你有什麼線索,爲什麼它不工作?編輯:也許**沒有... **應該是值0? –