我已经在我的平台上添加了一个内容分级系统,作者可以在其中选择他们的帖子适合的受众。目前,这些选项可用:
未评级
G
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\';
. 这怎么可能?理想情况下,在我的平台流量很大的情况下,服务器上的解决方案是轻量级的。