我在我的平臺上添加了一個內容評級系統,作者可以選擇他們的帖子適合哪些受衆。目前,這些選項可供選擇:顯示自定義字段選擇-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';'或者如果它是默認的(未評級),我想'回聲'內容評級:未評級';' –
我'我用一個例子更新了我的答案。 –
我剛剛測試了你的編輯,它將所有內容迴應爲「內容評級:未評級」。你有沙箱來自己測試嗎? –