创建内容分级脚本

时间:2012-11-04 18:59:20

标签: php wordpress rating article

我正在尝试编写内容评级脚本,用户可以在其中选择要为其文章提供的评级类型(例如,文章适合的年龄组)。

我使用Wordpress星级评分脚本作为模板。

脚本的这一部分是用户选择评级的地方:

function pn_apr_meta_box_form( $post )
{   
    wp_nonce_field( 'pn_apr_meta_box_nonce', 'pn_apr_meta_box_nonce_field' );

    $current_post_rating = get_post_meta( $post->ID, PN_APR_RATING_META_KEY, true );

    echo '<label for="pn_apr_rating">' . __( 'Choose a rating for this post:', 'author-post-ratings' ) . '</label> ';
    echo '<select name="pn_apr_rating" id="pn_apr_rating">';
    echo '<option value="unrated"' . selected( $current_post_rating, 0, false ) . '>' . __( 'Unrated', 'author-post-ratings' ) . '</option>';
    for ( $i = 1; $i <= 5; $i++ ) {
        echo '<option value="' . $i . '"' . selected( $current_post_rating, $i, false ) . '>' . sprintf( _n( '%1s Star', '%1s Stars', $i, 'author-post-ratings' ), $i ) . '</option>';
    }
    echo '</select>';
}

脚本的这一部分输出评级:

if ( $rating ) {

    $output = null;

    $output .= '<div class="author-post-rating">';

    $output .= '<span class="author-post-rating-label">' . esc_attr( $pn_apr_settings['label_text'] ) . '</span> ';

    $output .= '<span class="author-post-rating-stars" title="' . sprintf( __( '%1$d out of %2$d stars', 'author-post-ratings' ), $rating, 5 ) . '">';

    // Output active stars
    for ( $i = 1; $i <= $rating; $i++ ) {

        $output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-active.png" />';

    }

    // Output inactive stars
    for ( $i = $rating + 1; $i <= 5; $i++ ) {

        $output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-inactive.png" />';

    }

    $output .= '</span>' . "\n";

    $output .= '</div><!-- .author-post-rating -->';

    if ( true == $return ) { return $output; }

    // We don't need to use "else" here, since calling return will automatically stop further execution of this function.
    echo $output;

}

现在,我想更改此脚本,使其成为内容评级脚本(而非星级评分)。我想为用户提供以下选择:

  

G - 适合所有观众

     

PG - 可能是冒犯性的,通常是针对13岁及以上的观众

     

R - 适用于17岁以上的成年观众

     

X - 比上面更成熟

问题:如何更改脚本,以便在用户选择PG时,它将输出文本Suitable for age 13 and up

编辑:

To Shawn,请遵守以下代码:

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>';

    // build an array of each available rating
    $ratings = array(
        1 => 'G — Suitable for all audiences',
        2 => 'PG — Possibly offensive, usually for audiences 13 and above',
        3 => 'R — Intended for adult audiences above 17',
        4 => 'X — Even more mature than above'
    );

    echo '<select name="rating">';
    echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </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>';
}

1 个答案:

答案 0 :(得分:2)

这里有一些东西可以帮助你到达你需要的地方:

添加到您的functions.php:

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']);
    }

}

上面的代码执行此操作: 1.在帖子中添加新的元数据箱 2.添加一个带有评级值的选择框 3.使用帖子保存元数据

要访问模板中的元数据:

$meta = get_post_custom($post->ID);
echo $meta['rating'][0];

要让模板显示自定义字符串,请使用以下内容:

switch ( $meta['rating'][0] ) {
    case 0:
        echo "This is rated PG";
        break;
    case 1:
        echo "This is rated G";
        break;
    case 2:
        echo "This is rated R";
        break;
    case 3:
        echo "Ug oh! This is rated X!";
        break;
    default:
        echo "This is not yet rated.";
}

**编辑:此代码提供完整功能..如果适用于您,您可以放弃当前的解决方案