Created
February 10, 2017 20:05
-
-
Save mwordpress/a012280c0eb09774eb19599926d68519 to your computer and use it in GitHub Desktop.
the codes listed here is part of the following article : https://www.mwordpress.net/add-thumbnail-to-wordpress-without-custom-field/
This file contains 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
/* | |
* Get attachment id for default image url | |
* $default url | |
*/ | |
function get_attachment_id_from_src($image_src) { | |
global $wpdb; | |
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'"; | |
$id = $wpdb->get_var($query); | |
return $id; | |
} | |
/* | |
* Auto set featured image | |
* Note : image must be in post attachemnts | |
*/ | |
function autoset_featured() { | |
global $post; | |
$postsetid = get_the_ID(); | |
$already_has_thumb = has_post_thumbnail($post->ID); | |
$default = 'http://link-to-image’; | |
if (!$already_has_thumb) { | |
$attached_image = get_children('post_type=attachment&post_mime_type=image&post_parent='.$postsetid ); | |
if ($attached_image) { | |
foreach ($attached_image as $attachment_id => $attachment) { | |
set_post_thumbnail($post->ID, $attachment_id); | |
} | |
} else { | |
set_post_thumbnail($post->ID, get_attachment_id_from_src(get_attachment_id_from_src($default))); | |
} | |
} | |
} | |
add_action('the_post', 'autoset_featured'); |
This file contains 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
/* | |
* Get First Image in post content | |
* Note : the post content must contain one image at least | |
*/ | |
function getImage($num) { | |
global $more; | |
$more = 1; | |
$content = get_the_content(); | |
$count = substr_count($content, '<img'); | |
$start = 0; | |
for($i=1;$i<=$count;$i++) { | |
$imgBeg = strpos($content, '<img', $start); | |
$post = substr($content, $imgBeg); | |
$imgEnd = strpos($post, '>'); | |
$postOutput = substr($post, 0, $imgEnd+1); | |
$image[$i] = $postOutput; | |
$start=$imgEnd+1; | |
$cleanF = strpos($image[$num],'src="')+5; | |
$cleanB = strpos($image[$num],'"',$cleanF)-$cleanF; | |
$imgThumb = substr($image[$num],$cleanF,$cleanB); | |
} | |
if(stristr($image[$num],'<img')) { | |
echo $imgThumb; | |
} | |
$more = 0; | |
} |
This file contains 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
<div class="thumbnail"> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> | |
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php getImage('1'); ?>&w=150&h=150&zc=1"> | |
</a> | |
</div> |
This file contains 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
.thumbnail a:link, | |
.thumbnail a:visited { | |
display:block; | |
float:left; | |
padding:5px; | |
background:#e2e2e2; | |
width:150px; | |
height:150px; | |
margin:5px 5px 0 0; | |
} | |
.thumbnail a:hover, | |
.thumbnail a:active { | |
background:#C4C4C4; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment