- This topic has 8 replies, 4 voices, and was last updated 4 years, 5 months ago by David.
-
AuthorPosts
-
August 26, 2019 at 12:22 pm #994063Marcus Lindblad
Hi!
I am currently playing around with the Page Header and trying to find a way to add star ratings to some of my posts.
I found that you could use {{custom_field.name}} in the page header which makes it really scalable.
Problem:
I’ve created a little bit of HTML code to display the stars in the page header, but how can I make it so that I can control the amount of stars from every post? (With Custom Fields or something similar)August 26, 2019 at 4:28 pm #994189TomLead DeveloperLead DeveloperHi there,
How does your HTML code work? Does it take a variable (1,2,3,4,5 stars etc..) and build the number of stars?
If so, you could create a shortcode:
add_shortcode( 'star_ratings', function() { ob_start(); $stars = get_post_meta( get_the_ID(), '_your_star_rating_field_name', true ); if ( $stars ) { ?> Your HTML with $stars variable in here. <?php } return ob_get_clean(); } );
Then you can use [star_ratings] in your Hero.
Let me know π
August 27, 2019 at 12:08 am #994311Marcus LindbladHey again and thanks for your answer!
No, sorry – I have just done a very simple HTML code and styled it with CSS…<div class="review rating"> <div class="review subject">4 / 5</div> <div class="stars"> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="empty-star"> </span> </div> </div>
And then I added some basic CSS-styling to display it on the front end.
.filled-star:before { content: "β"; padding: 10px; margin: -10px; } .empty-star:before { content: "β"; filter: saturate(0.1); padding: 10px; margin: -10px; }
If you have a code example with variables please link it and I’ll try to learn π I guess that’s better practice than my code.
August 27, 2019 at 2:56 am #994378Marcus LindbladI got help from my master coder friend which recoded the snippet to look like this.
I’ll mark the thread as Solved but if anyone has any comments on improvements, please let me know!add_shortcode( 'star_ratings', function() { ob_start(); $stars = get_post_meta( get_the_ID(), 'review_stars', true ); if ( $stars ) { $starsInt = (int)$stars; ?> <div class="review rating"> <div class="review subject"><?php echo $stars; ?> / 5</div> <div class="stars"> <!-- if 0 stars --> <?php if($starsInt === 0) { ?> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <?php } ?> <?php if($starsInt === 1) { ?> <span class="filled-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <?php } ?> <?php if($starsInt === 2) { ?> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <?php } ?> <?php if($starsInt === 3) { ?> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="empty-star"> </span> <span class="empty-star"> </span> <?php } ?> <?php if($starsInt === 4) { ?> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="empty-star"> </span> <?php } ?> <?php if($starsInt === 5) { ?> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <?php } ?> <!--<span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="filled-star"> </span> <span class="empty-star"> </span>--> </div> </div> <?php } return ob_get_clean(); } );
August 27, 2019 at 8:12 am #994700TomLead DeveloperLead DeveloperLooks good! π
November 12, 2019 at 5:41 am #1060358Liliahy Marcus,
I tried to implant your code but unfortunately it doesn’t work for me !How and where did you implant Code?
Hopefully you can read my message.
best regards
lilNovember 12, 2019 at 6:24 am #1060390DavidStaffCustomer SupportHi there,
the code Marcus provided here is a PHP snippet that creates a shortcode. Adding PHP: https://docs.generatepress.com/article/adding-php/
Once the code is added you can the display the review stars anywhere with the
[star_ratings]
shortcodeYou will also need to include the CSS in Customizer > Additional CSS provided here
November 12, 2019 at 6:55 am #1060732LiliaHello David,
thank you very much for your message
i have implanted css and PHP snippet but unfortunately it doesn’t work on my Site, i implatet php code and css code with copy paste! what could I have done wrong?lil
November 12, 2019 at 7:08 am #1060745DavidStaffCustomer SupportFor it to work you have to:
1. Create a Custom Field named:
review_stars
in your post
2. Add the[star_ratings]
shortcode to the post -
AuthorPosts
- You must be logged in to reply to this topic.