Created
February 5, 2015 16:39
-
-
Save mauriciogofas/19fbef679ff9ef387628 to your computer and use it in GitHub Desktop.
Primeira imagem do post automaticamente como Imagem relacionada
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 | |
// 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