Created
September 24, 2024 20:05
-
-
Save undfine/17de821d498723467ef296b685aed708 to your computer and use it in GitHub Desktop.
Filters the output of the featured image for a custom post type, based on a taxonomy with a custom image field.
This file contains hidden or 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
| <?php | |
| /** | |
| * | |
| * Filter the thumbnail for Providers based on taxonomy | |
| * @param mixed $thumbnail_id | |
| * @param mixed $post | |
| * @return mixed | |
| */ | |
| function PREFIX_filter_provider_thumbnail($thumbnail_id, $post) | |
| { | |
| $post_id = $post->ID; | |
| $post_type = 'provider'; | |
| $taxonomy = 'specialty'; | |
| $taxonomy_thumb_meta = 'specialty_icon'; | |
| $default_id = 15164; // ID of the default thumbnail (attachment ID) | |
| // exit early if on backend, or not the correct post type, or thumbnail is already set | |
| if (is_admin() || $thumbnail_id || get_post_type($post_id) !== $post_type) | |
| return $thumbnail_id; | |
| // get terms for the current post: returns array of WP_Term Objects | |
| $terms_list = get_the_terms($post_id, $taxonomy); | |
| // check if there are any terms, and if not return the default | |
| if (!$terms_list || !is_array($terms_list)) | |
| return $default_id; | |
| // filter the $terms_list to only include top-level tems (i.e. "parent" = 0) | |
| $terms_list = array_filter($terms_list, function ($term) { | |
| return (int) $term->parent === 0; | |
| }); | |
| // get the first term | |
| $terms_list = array_values($terms_list); | |
| $first_term = array_shift($terms_list); | |
| // get the attachment_id for the first term | |
| $term_thumbnail_id = get_term_meta($first_term->term_id, $taxonomy_thumb_meta, true); | |
| // set the thumbnail | |
| $thumbnail_id = ($term_thumbnail_id) ? $term_thumbnail_id : $default_id; | |
| // output | |
| return (int) $thumbnail_id; | |
| } | |
| add_filter('post_thumbnail_id', 'PREFIX_filter_provider_thumbnail', 10, 2); | |
| // Register the column | |
| function PREFIX_add_taxonomy_column_hooks() | |
| { | |
| $taxonomy = 'specialty'; | |
| add_filter('manage_' . $taxonomy . '_custom_column', 'PREFIX_specialty_rows', 15, 3); | |
| add_filter('manage_edit-' . $taxonomy . '_columns', 'PREFIX_specialty_columns'); | |
| } | |
| add_action('admin_init', 'PREFIX_add_taxonomy_column_hooks'); | |
| function PREFIX_specialty_rows($row, $column_name, $term_id) | |
| { | |
| //get thumb | |
| $thumb_id = get_term_meta($term_id, 'specialty_icon', true); | |
| $thumb_img = ($thumb_id) ? wp_get_attachment_image($thumb_id, [60,60]) : ''; | |
| if ('thumb' === $column_name) { | |
| echo $thumb_img; | |
| } | |
| } | |
| function PREFIX_specialty_columns($original_columns) | |
| { | |
| $new_columns = $original_columns; | |
| array_splice($new_columns, 1); | |
| $new_columns['thumb'] = esc_html__('Thumb', 'taxonomy-images'); | |
| return array_merge($new_columns, $original_columns); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is currently using an example post_type of "provider" with a taxonomy called "specialty." This Taxonomy has the custom field "specialty_icon" which returns the attachment ID. If no featured image is found for the current post, it tries to replace it with the image set for the top-level term in that taxonomy. Can also set a default (attachment ID) if no image is set as featured, or if there is no image in the post terms.