문제

I am trying to write a content rating script where the user can select what type of rating to give to their article (for instance, what age group the articles suits for).

I am using a Wordpress star rating script as a template.

This part of the script is where the user selects the rating:

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

This part of the script outputs the ratings:

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;

}

Now, I want to change this script so that it becomes a content rating script (rather than star rating one). I want to offer these choices for the user:

G — Suitable for all audiences

PG — Possibly offensive, usually for audiences 13 and above

R — Intended for adult audiences above 17

X — Even more mature than above

Question: How can I change the script so that if the user selects for instance PG, then it will output the text Suitable for age 13 and up.

EDIT:

To Shawn, observe the following code:

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>';
}
도움이 되었습니까?

해결책

Here's something that should get you to where you need to be:

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

}

The above code does this: 1. Adds a new metabox to your posts 2. Adds a select box with the rating values 3. Saves the metadata with the post

To access your metadata in your templates:

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

To have your template display a custom string use something like:

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.";
}

**Edit: This code provides full functionality.. you could abandon your current solution if it works for you

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top