Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP to determine Stars

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #193388
    Jimmy
    Participant

    Hey guys,

    I have a custom post type and one of the fields is a post rating based on values {One, Two, …, Nine, Ten}.

    What I want to do is implement a visual star image-rating (**********) using however many star.svg’s is equivalent to the posts corresponding field rating value. I want to do this without using a plugin because I want to use my own SVG graphic. I have too many posts to change the field value from a string to numeric so I have a feeling it will be complicated.

    With that being said, does anyone have any suggestions or links to anything that might point me in the right direction by any chance?

    #193389
    Senff
    Participant

    Maybe something like this?

    $rating = [custom field value];
    
    switch ($rating) {
        case "One":
            $numberstars = 1;
            break;
        case "Two":
            $numberstars = 2;
            break;
        case "Three":
            $numberstars = 3;
            break;
        case "Four":
            $numberstars = 4;
            break;
        case "Five":
            $numberstars = 5;
            break;
        case "Six":
            $numberstars = 6;
            break;
        case "Seven":
            $numberstars = 7;
            break;
        case "Eight":
            $numberstars = 8;
            break;
        case "Nine":
            $numberstars = 9;
            break;
        case "Ten":
            $numberstars = 10;
            break;
    }
    
    for ($i = 1; $i <= $numberstars; $i++) {
       echo '<img src="star.svg">';
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.