Skip to content

Instantly share code, notes, and snippets.

@prionkor
Created August 25, 2020 12:09
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 prionkor/5207a953fbc9590acf104431f74719e2 to your computer and use it in GitHub Desktop.
Save prionkor/5207a953fbc9590acf104431f74719e2 to your computer and use it in GitHub Desktop.
caregory thumbnail plugin fix
<?php
/** ==================================================
* Update thumbnail meta data of a post to category thumbnail.
* Only updates if post doesn't have a thumbnail image set by user.
*
* @param int $post_id post_id.
* @since 2.00
*/
public function update_thumbnail( $post_id ) {
// Skip if current theme doesn't support post thumbnails
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
return;
}
// Skip post thumbnail update if thumbnail is already set in the post
if ( has_post_thumbnail( $post_id ) ) {
return;
}
$term_id = $this->choose_term( $post_id );
if ( 0 < $term_id ) {
$featured_image_id = intval( get_term_meta( $term_id, 'featured_image_id', true ) );
update_post_meta( $post_id, '_thumbnail_id', $featured_image_id );
} else {
delete_post_meta( $post_id, '_thumbnail_id' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment