I’m inserting images like this for a list of posts in wordpress:
<div>
<a href="<?php the_permalink(); ?>" alt="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
</div>
What I want that if has a class ‘full’ it inserts ‘large’ size post thumbnail instead of ‘medium’, like this:
<div class="full">
<a href="<?php the_permalink(); ?>" alt="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('large'); ?>
</a>
</div>
… and for ‘half’, ‘custom_medium_large’:
<div class="half">
<a href="<?php the_permalink(); ?>" alt="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('custom_medium_large'); ?>
</a>
</div>
Both .half and .large classes are applied via jQuery:
$(".home .grid div:first").addClass("full");
$('.home div:not(:first-child):nth-child(8n+2), .home div:not(:first-child):nth-child(8n+3)').addClass('half');
Any idea how do I make sure that container with .half or .full classes have corresponding image sizes than the default ‘medium’?
2
Answers
Open your theme’s functions.php file and Add the following code snippet to define a new thumbnail size:
To display the custom thumbnail size in a template, you can use the
If you want to apply a CSS class to the thumbnail itself, you can do so in your theme’s CSS file.
You’ll need to include image tags for each size, and toggle the display of the image you want based on the absence/presence of the CSS class. This may require some custom coding of the image tags, so that only the image you need is loaded, and not costing the user bandwidth.
Untested code, but should get you close:
You could also use jQuery to insert the image, with the URLs provided in a JSON object. That’s probably the better, more semantically correct approach.