admin管理员组

文章数量:1124643

Currently working on a Wordpress theme, and I can't for the life of me get my start rating widget to work.

I can call the function directly in the template via php, and via a short code, but my widget just won't show. I'm sure I'm missing something basic here.

star rating function

    // Render custom meta box for star rating
    function render_star_rating_meta_box() {
        global $post;
        wp_nonce_field( basename( __FILE__ ), 'star_rating_meta_box_nonce' );
        $star_rating = get_post_meta( $post->ID, '_star_rating', true );
        ?>
        <label for="star_rating"><?php esc_html_e( 'Star Rating:', 'mytheme' ); ?></label>
        <select name="star_rating" id="star_rating">
            <option value=""></option>
            <?php for ( $i = 0; $i <= 10; $i++ ) : ?>
                <?php $value = number_format( $i / 2, 1 ); ?>
                <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $star_rating, $value ); ?>><?php echo esc_html( $i / 2 ); ?></option>
            <?php endfor; ?>
        </select>
        <?php
    }

// Save star rating meta box data
function save_star_rating_meta_box( $post_id ) {
    if ( ! isset( $_POST['star_rating_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['star_rating_meta_box_nonce'], basename( __FILE__ ) ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
    if ( isset( $_POST['star_rating'] ) ) {
        $star_rating = sanitize_text_field( $_POST['star_rating'] );
        update_post_meta( $post_id, '_star_rating', $star_rating );
    } else {
        delete_post_meta( $post_id, '_star_rating' );
    }
}
add_action( 'save_post', 'save_star_rating_meta_box' );

// Display star rating on frontend
function display_star_rating() {
    $star_rating = get_post_meta(get_the_ID(), '_star_rating', true);
    
    if (!empty($star_rating)) {
        $rounded_rating = round($star_rating, 1); // Round to one decimal place
        echo '<div class="star-rating my-4" itemprop="reviewRating" itemscope itemtype=";>';
        for ($i = 1; $i <= 5; $i++) {
            if ($i <= $star_rating) {
                echo '<i class="fa fa-star" aria-hidden="true"></i>';
            } elseif ($i - 0.5 <= $star_rating) {
                echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
            } else {
                echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
            
            }
            
        }
            // Display numerical value
            echo '<span class="star-rating-value px-4"><span itemprop="ratingValue">' . esc_html($rounded_rating) . '</span>/5</span>';
            echo '</div>';
        
    }
}

widget in functions.php

class Star_Rating_Widget extends WP_Widget {
 
    function __construct() {
        parent::__construct(
            'star_rating_widget',
            __('Star Rating Widget', 'mytheme'),
            array('description' => __('Displays star rating', 'mytheme'))
        );
    }
 
    public function widget($args, $instance) {
        echo $args['before_widget'];
        echo '<div class="star-rating-widget">';
        echo '<h3 class="widget-title">' . __('Star Rating', 'mytheme') . '</h3>';
        // Call the function to display star rating
        if (function_exists('display_star_rating')) {
            display_star_rating();
        } else {
            echo 'display_star_rating() function is not available.';
        }
        echo '</div>';
        echo $args['after_widget'];
    }
 
    public function form($instance) {      
    }
 
    public function update($new_instance, $old_instance) {           
    }
}
 
function register_star_rating_widget() {
    register_widget('Star_Rating_Widget');
}
add_action('widgets_init', 'register_star_rating_widget');

本文标签: Why doesn39t my star rating widget work