添加下面的脚本到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? –