I'm building my first WordPress Theme and I'm stuck on something.
I have a function in my functions.php called get_first_photo() which grabs the first image uploaded on each post and puts it on the blog archive page. It's working fine, but it loads the full-sized image and resizes it using CSS. I would rather it load the image at it's thumbnail size set in the WP control panel so I don't have the image-size overhead. Any way to accomplish this?
Here's the code from functions.php:
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT guid FROM wp_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And here's the blog template:
<?php
get_header(); ?>
<div id="content" class="blog">
<div id="body">
<h3 class="title" id="blog">The Ned Leary Blog</h3>
<?php if (have_posts()) :
query_posts("category_name=blog");
while (have_posts()) :
the_post();
$first_photo = get_first_photo(get_the_ID());
?>
<div class="snippet">
<?php if (!empty($first_photo)) : ?>
<img src="<?php echo $first_photo; ?>" alt="Thumbnail" />
<?php else : ?>
<img src="<?php bloginfo('url'); ?>/images/blog/gravatarBig.jpg" alt="Ned Leary Gravatar" />
<?php endif; ?>
<div class="details">
<h2><?php the_title(); ?></h2>
<h5><?php the_time('D, M j, Y') ?> by <?php the_author('') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h5>
<?php the_excerpt(); ?>
<h4>Read moreā¦</h4>
</div>
</div><!--end snippet-->
<?php endwhile; endif;// end of the loop. ?>
</div><!--end body-->
<?php get_sidebar(); ?>
</div><!--end content-->
<?php get_footer(); ?>
All you need is to grab the post ID for that first image you want and then run it through "get_the_post_thumbnail()".
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo );
You can also pull your own custom thumbnail sizes if you like as well. As long as you are using wordpress 2.9+.
Simply add this to functions.php
add_theme_support( 'post-thumbnails' ); //enable thumbs
add_image_size( 'mycustom-preset', width you want, height you want); //custom size
Then run the same function but call your new preset...
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo, 'mycustom-preset' );
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
http://codex.wordpress.org/Function_Reference/add_image_size
Hope that helps. It doesn't seem like your having trouble obtaining the post id for the first photo so I didn't really cover how to obtain it.
You can use GD or some other image library to programmatically manipulate an image. However, it would be best to implement an operation to create a thumbnail file when the image is uploaded. You can have it dynamically generate a thumbnail every time the page is loaded, but such an operation can be relatively computationally expensive and put unneeded load on the ssystem.
Thanks to John Ford for pointing me in the right direction, I was able to figure this out.
The query in my function changed slightly, grabbing the post id instead of guid.
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT id FROM aire_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And then I had to add another line to my theme file:
$first_photo = get_first_photo(get_the_ID());
$thumb = wp_get_attachment_image_src($first_photo);
Lastly, I updated my image src:
<img src="<?php echo $thumb[0]; ?>" alt="Thumbnail" />
you could use the get_the_post_thumbnail function or use a php thumbnail generator like timbthumb
<?php $images = get_children( array( 'post_parent' => $page->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
if ( $images ) {
$total_images = count( $images );
$image = array_shift( $images );
$thumbnail = wp_get_attachment_image_src($image->ID, array(225,125) );
$thumbnail = $thumbnail[0]; }
?>
<img class="size-thumbnail alignleft" src="<?php echo $thumbnail; ?>" alt="<?php echo $page->post_title ?>">
Related
I'm using wp_query method to display all my woocommerce products, but I have applied Google's PageSpeed tool and found out that the images size are too large, and taking me longer to load.
I'm aware WordPress has duplication of images with different size that has been uploaded, but I don't know how to approach the size I want.
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => $cat,
'paged' => $paged
);
$loop = new WP_Query( $args );
if (have_posts()):
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="grid-item">
<a href="<?php echo get_permalink(); ?>">
<div style="position: relative;">
<img src="<?php echo the_post_thumbnail_url(); ?>" alt="<?php echo get_the_title() ?>"/>
<?php
if(!$product->managing_stock() && !$product->is_in_stock()) {
echo '<div class="out">OUT OF STOCK</div>';
}
if($product->is_on_sale()){
echo '<div class="sale">SALE</div>';
}
?>
</div>
<span class="title">
<?php echo get_the_title(); ?>
</span>
<span class="price">
<?php echo $product->get_price_html(); ?>
</span>
</a>
</div>
<?php
endwhile;
endif;
I want the images to be size 900x300.
How can I set images size using WP_Query arguments?
How to approach different sizes:
You need to insert the size's name (thumbnail, full, etc.) as a parameter inside the the_post_thumbnail_url() function. For example:
<?php the_post_thumbnail_url('thumbnail'); ?>
Note that the_post_thumbnail_url() echoes the url already, no need to echo it again. If you don't want the function to directly echo it, use get_the_post_thumbnail_url() instead. (Notice that the two functions require different sets and orders of parameters.)
How to set up a size:
The default sizes are thumbnail, medium, large, and full.
None of them have a dimension of 900 x 300.
You can either go to Settings->Media in your WP dashboard and change one of the settings, or you can add the following in your functions.php:
add_image_size('NEW_NAME', 900, 300);
Read about the parameters of add_image_size() function if you need any customization.
So I tried to make a custom post type called 'showcase' and created a few posts in this custom post type. The problem is that when I click on a 'product' it should showup in a lightbox instead of going to the thumbnail url.
This is the code I am using:
<section id="showcase">
<?php
$args = array( 'post_type' => 'showcase', 'posts_per_page' => -1 ); $the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
$it = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$terms = get_the_terms( $post->ID , 'showcase_category');
$it ++;
if ($it == 6) {
echo '</section><div class="cta-block"><h2>Live model drawing</h2><p>Drawing from a live model, gives you the opportunity to draw what you see instead of drawing what you think about.</p>View courses</div><section id="showcase">';
}
?>
<a class="entry-showcase" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
<?php wp_reset_postdata();
endwhile; else :
endif; ?>
</section>
Also, I tried using a few plugins which automaticly detect jpg, gif etc but for some reason they are not working for thumbnail images.
Some plugins I tested:
Easy FancyBox,
Responsive Lightbox,
Simple Lightbox,
Also, I am not getting any errors it just goes straight to the media file in url instead of staying on the same page and showing the image in a lightbox.
Thanks in advance (:
You can maybe use the Fresco-Lightbox. After implementing you can add the "fresco"-class to any imagelink and open it in a lightbox:
<a class="entry-showcase fresco" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
Also see the Fresco documentation.
i have a custom post type-> events. I have a gallery page in which the featured image from a custom post is used as album cover . Now when a person clicks on the album cover i.e featured image , i want to display images in attached gallery in the post. I have been using the following code to display the featured images in the main gallery page .But how do i show the rest of the images of the post. Please help me out.
<?php $paged= (get_query_var('paged' )) ? get_query_var('paged'):1; ?>
<?php $args = array( 'post_type'=>'events',
'posts_per_page' => 100 ,
'paged'=>$paged
);
$loop = new WP_Query( $args );
if($loop->have_posts()):while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array(225,225) ); ?>
<?php if(!$image) :continue; else:?>
<div class="col-lg-3 col-md-3 col-sm-3 gallerycol paddingdel-l">
<div class="about-img">
<div class="about-img-bot">
<div class="photo-quanity"></div>
</div>
<img class="img-responsive" src="<?php echo $image[0]; ?>" alt="" />
</div>
<div class="clearfix"></div>
<p><?php the_title(); ?> </p>
</div>
<?php endif; ?>
<?php endwhile;endif; ?>
If i put the featured image in the anchor tag with href="the_permalink" then it takes me to the template single-events.php which is having a lot of infomation . How do i display only the images in the attached gallery
Any hint will be really helpful and appreciable
I am working on the home page of this site.
I have successfully placed a featured image thumbnail of the latest blog post in the 3rd column on the right. I am not trying to make the first column featured image thumbnail come from a custom post type I've created called portfolio_item
I've used the following code to place the blog post featured image:
<?php query_posts('showposts=1'); ?>
<?php while (have_posts()): the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); ?>
<img class="aligncenter circle" alt="" src="<?php echo $image[0]; ?>" />
<?php endif; ?>
<?php endwhile;
wp_reset_query();
?>
What do I need to change to make it retrieve the featured image for the latest post in the custom post type portfolio_item?
query_posts(array ( 'showposts' => 1 , 'post_type' => 'portfolio_item') );
Can someone help me change this theme to pull the images in the sidebar from the custom field "sidebar" of each post, instead of the featured image, like it is now. The frame and link can stay the same, I just need the image to come from the custom field instead of the featured image.
<div id="content" class="city_page">
<div id="inner_sidebar">
<?php
$mypages = get_pages( array( 'child_of' => 9, 'sort_column' => 'post_title', 'sort_order' => 'asc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<div class="item city">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
<img class="frame" src="<?php bloginfo('url'); ?>/images/city_image_frame_thumb.png" alt="" />
<a class="title" href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
</div>
<?php
}
?>
</div>
As long as your are placing an image URL in the "sidebar" custom field you can replace the entire img src with the following.
<?php echo get_post_meta($post->ID, 'sidebar', true) ?>
You can learn more about the get_post_meta for thumbnail urls here: http://codex.wordpress.org/Function_Reference/get_post_meta#Retrieve_a_Custom_Field_Thumbnail_Url
Hope that helps!
Personally, I am a huge fan of the Advanced Custom Fields plugin. It is one of the first plugins I install in a new WordPress installation. It is really flexible and has a simple API for getting and setting custom fields.
Check it out!