Skip to content

Instantly share code, notes, and snippets.

@mauriciogofas
Created February 5, 2015 16:39
Show Gist options
  • Save mauriciogofas/19fbef679ff9ef387628 to your computer and use it in GitHub Desktop.
Save mauriciogofas/19fbef679ff9ef387628 to your computer and use it in GitHub Desktop.
Primeira imagem do post automaticamente como Imagem relacionada
<?php
// Imagem relacionada é a Primeira imagem do post
function ds_get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/wp-content/themes/theme/images/default.jpg";
}
return $first_img;
}
// Fim Imagem relacionada é a Primeira imagem do post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment