Skip to content

Instantly share code, notes, and snippets.

@turtlepod
Last active December 30, 2018 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlepod/0642d19439de6b96da06342619ed705a to your computer and use it in GitHub Desktop.
Save turtlepod/0642d19439de6b96da06342619ed705a to your computer and use it in GitHub Desktop.
Listify Snippet: Add Listing Category in Listing Card
<?php
/**
* Add Category List To Listing Data.
*
* @link https://gist.github.com/turtlepod/0642d19439de6b96da06342619ed705a
*/
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.
*
* @link https://gist.github.com/turtlepod/0642d19439de6b96da06342619ed705a
*/
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 '<# } #>';
} );
@turtlepod
Copy link
Author

@Adrianoweb1
Copy link

very good...
I wanted a Add Listing Category in Listing Card code to display the "company_website" link from "job_listing-url". click and send the user direct to external site. Can you create?

@jordiroscamps
Copy link

Hi! it's perfect to show the listing category, but if i also want to show the listing type of job, what code i have to write? Thanks and sorry for my poor english.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment