Skip to content

Instantly share code, notes, and snippets.

@pickplugins
Last active September 21, 2022 04:35
Show Gist options
  • Save pickplugins/d15936783ff2ed90315b383993052272 to your computer and use it in GitHub Desktop.
Save pickplugins/d15936783ff2ed90315b383993052272 to your computer and use it in GitHub Desktop.
remove_action('related_post_loop_item_element_post_thumb', 'related_post_loop_item_element_post_thumb', 10, 2);
add_action('related_post_loop_item_element_post_thumb', 'related_post_loop_item_element_post_thumb_20200126', 10, 2);
function related_post_loop_item_element_post_thumb_20200126($loop_post_id, $elementData)
{
//$option_id_value = isset($elementData['option_id']) ? $elementData['option_id'] : '';
//$post_meta_value = get_post_meta($loop_post_id, 'meta_key', true);
$html_thumb = '';
$thumb_linked = 'yes';
$item_post_permalink = get_permalink($loop_post_id);
$post = get_post($loop_post_id);
$post_content = $post->post_content;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);
if (!empty($matches[1][0]))
$first_img = isset($matches[1][0]) ? $matches[1][0] : '';
if (empty($first_img)) {
$html_thumb .= '';
} else {
if ($thumb_linked == 'yes') {
$html_thumb .= '<a href="' . $item_post_permalink . '"><img src="' . $first_img . '" /></a>';
} else {
$html_thumb .= '<img src="' . $first_img . '" />';
}
}
echo $html_thumb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment