vendredi 26 avril 2019

How to display multiple consecutive html elements

I'm using ACF on my wordpress site to display HTML text in the search bar next to post titles based on that posts array values.

Right now the script only displays up to 1 of the html values, but I want it to include all of the values if they exist.

add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); 
function asp_custom_field_to_results( $results ) {
  $custom_field = 'trade_status'; 

  foreach ($results as $k=>&$r) {
    if ($r->content_type != 'pagepost') continue;
    if ( function_exists('get_field') )
        $trade_status  = get_field( $custom_field, $r->id, true ); // ACF support
    else
        $trade_status  = get_post_meta( $r->id, $custom_field, true );
    // Modify the post title to add the meta value
    if ( !empty($trade_status) ) {
      if ( in_array('30', $trade_status) ) {
        $html = '<span class="new">New</span>';
      } else if ( in_array('20', $trade_status) ) {
        $html = '<span class="active">Active</span>';
      } else if ( in_array('10', $trade_status) ) {
        $html = '<span class="closed">Closed</span>';
      } else {
        $html = '';
      }
      $r->title = $html . $r->title;
    }
  }

  return $results;
}

Aucun commentaire:

Enregistrer un commentaire