我在我的平台上添加了一个内容评级系统,作者可以选择他们的帖子适合哪些受众。目前,这些选项可供选择:显示自定义字段选择-WordPress
- 未评级
- 摹
- PG
- [R
,我用它来显示文章的编辑页面上的评分选项的代码是:
// Article Content Rating
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
__('Content Rating (optional)'), // meta box title
'rating_select_cb', // callback function, spits out the content
'post', // post type or page. This adds 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>Article Content Rating: </label>';
$ratings = array(
1 => ' G ',
2 => ' PG ',
3 => ' R ',
);
echo '<select name="rating">';
echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> Unrated </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>';
}
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']);
}
}
// END Article Content Rating
现在,问题是,我添加了哪些代码single.php
来显示他们的选择?举例来说,如果作者选择了PG,那么我想要echo 'Content Rating: PG';
或者如果它是默认的(未评级),我想要echo 'Content Rating: Unrated';
。这怎么可能?理想情况下,由于我的平台流量很大,所以服务器上的解决方案很轻。
请你详细说明我将如何根据我想要的回应它。例如,如果作者选择了PG,那么我想'回声'内容评级:PG';'或者如果它是默认的(未评级),我想'回声'内容评级:未评级';' –
我'我用一个例子更新了我的答案。 –
我刚刚测试了你的编辑,它将所有内容回应为“内容评级:未评级”。你有沙箱来自己测试吗? –