Last active
January 31, 2018 14:58
-
-
Save FernandoSalinas33/dcfd4296f6d685e379cd15a830897b5d to your computer and use it in GitHub Desktop.
Display Categories on Listing Card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add Category List To Listing Data. | |
*/ | |
add_filter( 'listify_get_listing_to_array', function( $data, $listing ) { | |
// Default value for categories. | |
$categories = array(); | |
// Get post terms for listing categories. | |
$terms = get_the_terms( $listing->get_object(), 'job_listing_category' ); | |
// Check if term exists. | |
if ( $terms && ! is_wp_error( $terms ) ) { | |
// Loop for each terms. | |
foreach( $terms as $term ) { | |
// Add only the name of the term in the list. | |
$categories[] = $term->name; | |
} | |
} | |
// Add in listing data, format it as comma separated. | |
$data['categories'] = implode( ', ', $categories ); | |
// Return modified list of data. | |
return $data; | |
}, 10, 2 ); | |
/** | |
* Display the category data in listing card. | |
* | |
*/ | |
add_action( 'listify_content_job_listing_meta', function() { | |
// Check if available. | |
echo '<# if ( data.categories ) { #>'; | |
// Output inside a <div> | |
echo '<div class="my-listing-categories">{{{data.categories}}}</div>'; | |
// End check. | |
echo '<# } #>'; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment