Advertisement
salmancreation

Display yellow stars based on rating + Metadata

Sep 28th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <!-- Display yellow stars based on rating -->
  2.                 <strong>Rating: </strong>
  3.                 <?php
  4.                 $nb_stars = intval( get_post_meta( get_the_ID(), 'movie_rating', true ) );
  5.                 for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
  6.                     if ( $star_counter <= $nb_stars ) {
  7.                         echo '<img src="' . plugins_url( 'Movie-Reviews/images/icon.png' ) . '" />';
  8.                     } else {
  9.                         echo '<img src="' . plugins_url( 'Movie-Reviews/images/grey.png' ). '" />';
  10.                     }
  11.                 }
  12.                 ?>
  13.  
  14.  
  15. DISPLAYING METADATA LINK
  16. Metadata can be retrieved easily using the get_post_meta() function. In our example above, we saved a post meta field named product_price. We can retrieve the value of this field for a given post using the following code:
  17. <?php
  18.   // If we are in a loop we can get the post ID easily
  19.   $price = get_post_meta( get_the_ID(), 'product_price', true );
  20.  
  21.   // To get the price of a random product we will need to know the ID
  22.   $price = get_post_meta( $product_id, 'product_price', true );
  23. ?>
  24.  
  25. <?php
  26.     $args = array(
  27.       'post_type' => 'product',
  28.       'tax_query' => array(
  29.         array(
  30.           'taxonomy' => 'product_category',
  31.           'field' => 'slug',
  32.           'terms' => 'boardgames'
  33.         )
  34.       )
  35.     );
  36.     $products = new WP_Query( $args );
  37.     if( $products->have_posts() ) {
  38.       while( $products->have_posts() ) {
  39.         $products->the_post();
  40.         ?>
  41.           <h1><?php the_title() ?></h1>
  42.           <div class='content'>
  43.             <?php the_content() ?>
  44.           </div>
  45.         <?php
  46.       }
  47.     }
  48.     else {
  49.       echo 'Oh ohm no products!';
  50.     }
  51.   ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement