• Hopefully I can explain this. I have custom post types for movie reviews and book reviews. I have a custom taxonomy (behave as tags) called Rating with the following values: 1, 2, 3, 4, 5

    I want to output the rating I choose as an image. For example, if I rate a movie/book as 1, it will output an image of 1 star. If I rate a book/movie as 4 it will output an image of 4 stars (and so on).

    What I have right now is:
    <?php echo get_the_term_list( $post->ID, 'rating', 'Rating: ', ', ', '' ); ?></li>

    Which displays the number of the rating, not an image (obviously).

    How do I go about doing this? I have been trying for weeks (maybe months) off and on to no avail. I’d appreciate any help in the right direction.

Viewing 7 replies - 1 through 7 (of 7 total)
  • How about the “Star Rating for Reviews” plugin?

    Michael

    Thread Starter Jme

    (@6zzle)

    Thanks but I don’t want to use a plugin. I want to use tags. I know this is possible, I just haven’t a clue on how to go about it.

    instead of your line:

    <?php echo get_the_term_list( $post->ID, 'rating', 'Rating: ', ', ', '' ); ?>

    try and use:

    <?php $terms = get_the_terms($post->ID, 'rating');
    foreach($terms as $term) {
    echo '<img src="' . get_bloginfo('template_url') . '/images/rating-' . $term->slug . '.jpg" alt="" />';
    break; //remove this line if you want to show any possible number of tags
    }
    ?>

    assuming that you have created images rating-1.jpg to rating-5.jpg
    and saved them in the /images folder of your theme.
    also assuming that the slugs of your taxonomy tags are 1 to 5.
    and further assuming that you only want to show the first of the tags.

    Thread Starter Jme

    (@6zzle)

    Thank you so much! You are amazing!

    Thread Starter Jme

    (@6zzle)

    Ok now what I would like to do is output different text per rating. That wouldn’t be hard except, I’m using one taxonomy (Rating) for multiple post types.

    So, what I would like to do it output different text per rating for each different type of post type. Sounds confusing probably but hopefully I can explain.

    So a Rating of 2 for a book review, I would like to output something like “Not recommended. If you must, borrow it!” where if it was a Rating of 2 for a movie review, I’d like to output “Don’t pay to see it in a theatre but okay to rent”.

    Here is what I have that IS WORKING but I feel like there is a better/fasterr way. Is there?

    function get_rating () {
    
    $terms = get_the_terms($post->ID, 'rating');
    $type = get_post_type();
    
    foreach($terms as $term) {
    
    echo '<img src="' . get_bloginfo('template_url') . '/images/rating-' . $term->slug . '.png" alt="Rating" />';
    
    	if ($term->slug == "1") {
    		if ($type == "movie_review") { echo "Waste of time and money.";}
    		elseif ($type == "book_review") { echo "Waste of time and money.";}
    		else { echo "";}
    	}	
    
    	if ($term->slug == "2") {
    		if ($type == "movie_review") { echo "Not recommended.";}
    		elseif ($type == "book_review") { echo "Not recommended.";}
    		else { echo "";}
    	}	
    
    	if ($term->slug == "3") {
    		if ($type == "movie_review") { echo "Toss up. Rent it if you must!";}
    		elseif ($type == "book_review") { echo "A decent read.";}
    		else { echo "";}
    	}	
    
    	if ($term->slug == "4") {
    		if ($type == "movie_review") { echo "Must see but not worth owning.";}
    		elseif ($type == "book_review") { echo "Must read but you probably won't read it again.";}
    		else { echo "";}
    	}
    
    	if ($term->slug == "5") {
    		if ($type == "movie_review") { echo "Must watch and buy!";}
    		elseif ($type == "book_review") { echo "Must read and buy! You will read it again. ";}
    		else { echo "";}
    	}	
    
    break; //remove this line if you want to show any possible number of tags
    
    }
    
    }

    Any help is appreciated ?? Thanks in advance!

    How did it go? Did it work out?

    Thread Starter Jme

    (@6zzle)

    Worked out perfectly. You can see it in action at https://everysongends.com

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Replace tag with image’ is closed to new replies.