Jump to content

Inject dynamic output to a banner field


Alejandro

Recommended Posts

Say you need to make a call to an API and automatically show the information in your listing (price, inventory, etc.). You could code a solution directly into the PHP format of a custom field, but there's also a way to do this via hooks that may be more practical. 

First create an empty banner custom field with no description and the following php output format so the field is hidden by default.

return $text ?: false;

Then you can use the post list page and detail page query filter hooks to add an output for the banner field. 

Clickfwd\Hook\Filter::add('post_get_listings_listpage_query', function($listings, $params) 
{
  foreach($listings as & $listing) {
  	$listing['Field']['pairs']['jr_bannerfield']['description'] = 'your fantastically generated output';
  }

  return $listings;
});

Clickfwd\Hook\Filter::add('post_get_listing_detailpage_query', function($listing, $params)
{
  $listing['Field']['pairs']['jr_bannerfield']['description'] = 'your fantastically generated output';

  return $listing;  
});

If you do use an external API for this, making sure your calls are efficient or cached is outside the scope of this tip and the implementation is entirely up to the reader 😎.

Link to comment
×
×
  • Create New...