Skip to content
Advertisement

PHP: For loop, how do I set a variable as a maximum?

I am trying to be able to make a loop that lets me place stars on a website, to show a rating (1 to 5) on the website, that you can put down with fields. However, I do not know how to convert the field correctly.

I am using WordPress with Custom Field types, this is were I define the variable.

I have a variable that I get from somewhere else, and I have tried the following two ways:

JavaScript

and the second one:

JavaScript

Thank you very much for you help in advance.

Advertisement

Answer

First the_field echos the value, you need get_field to return the value. Then just repeat whatever string you’re using to display a star or empty star:

JavaScript

With a loop just check if your counter is less than or equal to the star rating:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement