To grab and resize thumbnail, I use aqua resizer from this link:
https://github.com/sy4mil/Aqua-Resizer
Call the thumbnail to show with this code on the loop:
<?php $thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' );
$image = aq_resize( $img_url, 150, 700, true );
?>
<img src="<?php echo $image ?>" width="150" height="700" alt="<?php the_title(); ?>"/>
It is working well. But only for feature image.
I want to set the caller not for feature image, but also for first post image.
So when I forget to set feature image on my post, the first image will show up as the thumbnail.
I know the code flow should be like this:
if(has_post_thumbnail()) {
// resize post thumbnail here e.g. $img_url = aq_resize...
} elseif($first_img) {
// resize the first img here, $img_url = aq_resize($first_img, ...
} else {
// $img_url = ''; //empty
}
But I'm new on php. Can anybody help?
Thanks in advance
You could put this function in your functions.php, and then call it from anywhere. It will return the source attribute of the first image tag it finds in your post, or a blank string if it doesn't find anything.
function get_first_image_src()
{
$content = get_the_content();
$image_regex = "/<img [^>]*src=[\"']([^\"^']*)[\"']/";
preg_match($image_regex, $content, $match);
if (empty($match))
return "";
return $match[1];
}
Related
I have setup a custom template and functions.php such that which users can insert an image url. The is for this image to be displayed in the header.
In the custom template, I use an add_action to place the image into the header. My problem is that whilst I can supply a url directly in the custom template with this image showing up, the same url inserted as a variable does not work.
So, the following extract from my custom template works (where $featimage is set to the url of the image)
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage="http://etc.jpg";
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
but here where $featimgage is set to the variable from the metabox it does not work.
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
p.s. I am using the function img() as in this post as without it, inserting $featimage directly into the src, even if pre-defined as the url, did not work.
I know that the variable options from them the metaboxes are working as images etc which I want displaying under the header all function.
Is this something to do with the order of wordpress functions? Thanks!
the img() function does not know about $memoptions variable as the variable is defined outside the scope of the img() function.
you can define $memoptions in img() function as below:
function img(){
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}
i am using dynamic featured image and adding multiple product images to a single custom post type name as products for a single product but and i am trying to get those images in my template but the array only return me only two sizes [thumb] [full] but i need medium as well below is my code
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
As you guys can see in the anchor tag $featured_image['medium'] this is how i want to echo this anchor tag but unfortunately it don't return me the medium size and i need help in getting the medium size as well. below is the array that i get where you can clearly see only [thumb] and [full]. please help
Array
(
[thumb] => http://www.example.com/wp-content/uploads/2014/07/product-1-120x90.jpg
[full] => http://www.example.com/wp-content/uploads/2014/07/product-1.jpg
[attachment_id] => 254
)
You need to get medium sized image by calling get_image_url function. Try this:
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
$mediumSizedImage = $dynamic_featured_image->get_image_url($featured_image['attachment_id'], 'medium');
echo "<img src = '" . $mediumSizedImage . "' />";
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
All available functions are documented here.
PS: I am author of the plugin.
I am working on a portfolio website. Whenever a portfolio piece is clicked, the user is taken to a page that shows details about that piece (i.e. more photos and information). There will also be previous and next navigation links to get to additional pieces. However, I want the previous and next navigation links to be a thumbnail photo of the next piece (custom field for that is thumbnail_photo). This is what I have so far:
<?php
$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'materials', $single = true);
$next_value = get_post_meta( $next_post->ID, 'thumbnail_photo', $single = true);
?>
<p><?php echo $prev_value; ?></p>
<p><?php echo $next_value; ?></p>
I used 'materials' in the call for $prev_value since 'materials' is another custom field. I just wanted to see if it was actually working. It outputs the materials just fine, but it only outputs the ID number of the thumbnail_photo. I can't get it to reference the file name so that I can output the actual image.
I am using the Advanced Custom Fields plugin, so each image is stored as an image object. So this is how I would typically output a thumbnail image:
<?php
$image = get_field('thumbnail_photo);
echo $image[sizes]["thumbnail"]; // thumbnail is a reference to wordpress' thumbnail media size
?>
When you configure the ACF input field, be sure you set the return value to be an image object instead of the image ID after change this, or if you already changed, you have to update the post or posts you're trying to get. Because, if you set the field return value as image ID, created the posts and now you wanna change their values you have to modify all the posts because every post_meta is containing the ID of the image.
In the ACF website has a tutorial how you could get the values an turn into images:
With the ID
<?php
wp_get_attachment_image( $next_value, 'thumbnail' );
?>
Another Example With the ID, but this time you have to create the image HTML, the function returns an array with the url, width and height
<?php
$image = wp_get_attachment_image_src( $next_value, 'thumbnail' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
You could do a check for the returned value and then write the image, example:
<?php
if( is_int( $next_value ) ) {
wp_get_attachment_image( $next_value, 'thumbnail');
} elseif ( is_object( $next_value ) ) {
echo $next_value['sizes']['thumbnail'];
} else { ?>
<img src="<?php the_field('field_name'); ?>" alt="" />
<?php } ?>
I didn't test it, but I think it works fine.
I hope this help and sorry for the bad english
In a wordpress site, i have defined a default NOT_FOUND_IMAGE in timthumb config file:
// "http://example.com/timthumb-config.php".
<?php
if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', 'http://example.com/img/default.jpg');
Is there any way to force resizing of this image depending on timthumb request parameters. For instance:
// 404 occurs
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2F404-image.jpg&h=180&w=120
// get resized (cropped) default image
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2Fdefault.jpg&h=180&w=120
Try this code:
if (!defined('NOT_FOUND_IMAGE'))
define ('NOT_FOUND_IMAGE','images/ingredient.jpg');
I don't rate timbthumb, and I think it has security issues. Install and use the ThumbGen plugin instead.
You can then use it really easily with a normal editable conditional php statement.. (Pseudo)
<?php if you have a thumbnail {
Output the img with thumbgen resized
} else {
Output the no image, you can even use thumbgen if you want
}
?>
Real world example:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<img src='<?php thumbGen($image_url,175,175, "crop=1"); ?>' />
<?php
}
else
{
?>
<img src="<?php bloginfo('template_url');?>/images/no-photo.png" alt="No Photo!">
<?php
}
?>
I am using this code to have a simple gallery on the page:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
The problem now is that the end-user has to upload an image via the Media page before selecting this image as featured image.
I know this could be solved by adding the featured image's ID to the shortcode's exclude list, but how to get this ID automatically?
function exclude_thumbnail_from_gallery($null, $attr)
{
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID
// temporarily remove the filter, otherwise endless loop!
remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
// pop in our excluded thumbnail
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;
// now manually invoke the shortcode handler
$gallery = gallery_shortcode($attr);
// add the filter back
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
// return output to the calling instance of gallery_shortcode()
return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
<?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?>
How about?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]');