WordPress.org

Ready to get started?Download WordPress

Forums

Replace tag with image (8 posts)

  1. Jme
    Member
    Posted 3 years ago #

    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.

  2. Michael
    Member
    Posted 3 years ago #

    How about the "Star Rating for Reviews" plugin?

    Michael

  3. Jme
    Member
    Posted 3 years ago #

    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.

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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.

  5. Jme
    Member
    Posted 3 years ago #

    Thank you so much! You are amazing!

  6. Jme
    Member
    Posted 3 years ago #

    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!

  7. mariosoriano
    Member
    Posted 3 years ago #

    How did it go? Did it work out?

  8. Jme
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic