My WordPress Blog consists of the following snippet:
<?php
$images = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
if (count($images) > 0) { ?>
<div class="image-holder">
<img src="<?php bloginfo('template_directory') ?>/javascript/timthumb.php?src=<?php echo $images[0]; ?>&h=320&w=630&zc=1" alt="<?php the_title();?>" />
</div>
<?php }
?>
This displays a title image (featured image) on top of the actual content and uses timthumbb to resize it. All is fine here, however it shows a broken img src if there is no defined featured image. Is there a way to edit this in order to only display the image if there is actually one present?
if there is only one image, you can use
if ($images[0]!='') {
I used this and it works fine:
// get the ancestors
$familyTree = get_ancestors($post->ID, 'page');
array_unshift($familyTree, $post->ID); //add the current page to the beginning of the list
// loop through the family tree until you find a result or exhaust the array
$featuredImage = '';
foreach ($familyTree as $family_postid) {
if (has_post_thumbnail($family_postid)) {
$featuredImage = get_the_post_thumbnail($family_postid, 'full');
// the 'full' argument tells WordPress not to re-size the image to thumbnail dimensions
break;
}
}
// if the page has a featured image then show it
echo ($featuredImage ? $featuredImage : "");
it also applies the featured image to child pages unless the child page has it's own featured image, not sure if you want that but shouldn't be too difficult to not show anything if there is no featured image.
Try using this logic
<?php
if($images[0]!='')
{
<div class="image-holder">
<img src="<?php bloginfo('template_directory') ?>/javascript/timthumb.php?src=<?php echo $images[0]; ?>&h=320&w=630&zc=1" alt="<?php the_title();?>" />
</div>
}
?>
You can use the function has_post_thumbnail() to check whether the post has a featured image or not.
Usage:
$images = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
if (count($images) > 0) { ?>
<div class="image-holder">
<img src="<?php bloginfo('template_directory') ?>/javascript/timthumb.php?src= <?php echo $images[0]; ?>&h=320&w=630&zc=1" alt="<?php the_title();?>" />
</div>
<?php }
} ?>
Related
So I'm using ACF Image: https://www.advancedcustomfields.com/resources/image/, and outputting my image and caption as an Array.
I'd like to be able to add links within my captions e.g. '', but when I do this it literally outputs the code rather than turning the text into a link.
Is there a way to make it be a link rather than just outputting the HTML as text.
This is my code for my image:
<?php
$image = get_sub_field('image'); if( $image ):
// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
?>
<img class="img-fluid" src="<?php echo esc_url($url); ?>" alt="<?php echo esc_attr($alt); ?>" />
<?php if( $caption ): ?>
<p class="caption"><?php echo esc_html($caption); ?></p>
<?php endif; ?>
<?php endif; ?>
But when I add this to the caption box within Wordpress: This image is from Flickr.
It literally outputs the same as above rather than turning the word into a link.
As you can see above I had the code:
<?php echo esc_html($caption); ?>
When I removed the 'esc_html' part then I was able to add links within the caption area. So the code for the Caption was this instead:
<?php if( $caption ): ?>
<p class="caption"><?php echo ($caption); ?></p>
<?php endif; ?>
What is the best method to replace the image in the footer for WordPress given the following code in the footer.php in WordPress? I would like to have a different image from the "main_logo". Keep in mind I have uploaded the image to the Media section of WordPress, and it has provided me with the URL for that image.
<?php if (!function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Bottom 1')) : ?><?php endif; ?>
<?php
$logo = $data['main_logo'];
$disable_footer_logo = $data['disable_footer_logo'];
?>
<?php if($disable_footer_logo!="Yes") : ?>
<?php
if($logo!=""){
$img_url = $logo;
}else{
$img_url = get_template_directory_uri().'/images/logo2.png';
}
?>
<img src="<?php echo $img_url; ?>" alt="" class="img-left" />
<?php endif; ?>
replace $logo = $data['main_logo']; with $logo = get_template_directory_uri().'/images/your_image.png', Assuming your image is located in images folder.
My theme generate my img portfolio with this code :
<img width="222" height="440" src="#" class="attachment-portfolio" alt="#">
I found this code :
<div class="carousel-inner">
<?php foreach($attachments as $attachment) {
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_big = wp_get_attachment_image( $attachment->ID, 'portfolio' ); ?>
<div class="item">
<?php echo $image_big ?>
</div>
<?php } ?>
I'm not good in PHP but i understand that the image size is generate from "$image_big" ?
I found also this in my function.php
add_image_size('big',9999,400);
I try to change the width and height in the add_image_size but nothing happend on the website ! It's still width="222" height="440"
I would like the image to be 100% or around 800px width (if it's impossible to get % unit). What to do ?
I have an Html page in which There is a slider. here's the html code
<div data-thumb="images/slide1.jpg" data-src="images/slide1.jpg">
</div>
<div data-thumb="images/slide2.jpg" data-src="images/slide2.jpg">
</div>
<div data-thumb="images/slide3.jpg" data-src="images/slide3.jpg">
</div>
I am trying to convert this page into wordpress. Here's how I am doing this in wordpress to load the images.
<?php
$options = array(
'post_type' => "slideshow",
);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div data-thumb="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>" data-src="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>">
</div>
<?php endwhile; ?>
But It is not working.My pictures are not showing in the slider. But When I check the page source It is correctly getting the image.
here's the source that generates
<div data-thumb="<img width="1170" height="385" src="http://localhost/wordpress/wp-content/uploads/2014/09/slide1.jpg" class="attachment-full wp-post-image" alt="slide1" />"
data-src="<img width="1170" height="385" src="http://localhost/wordpress/wp- content/uploads/2014/09/slide1.jpg" class="attachment-full wp-post-image" alt="slide1" />">
</div>
Change this:
In the first line of your "while" loop insert this:
if ( has_post_thumbnail() ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( the_ID()));
}
// else {
// you should see what to do if the post hasn't got a thumbnail
//
And then replace this:
<div data-thumb="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>"
for this:
<div data-thumb="<?php echo $image[0];?>"
Same thing in your second "if".
it's not the prettiest code, but it should work.
Calling the_post_thumbnail() prints the whole <img> tag, and you need only the URL to populate the DATA-SRC and DATA-THUMB attributes of your existing IMGs.
So you get the post thumbnail ID with get_post_thumbnail_id(), and get the image url with wp_get_attachment_src().
Hope it helps.
Spent a while on this now with little to no avail.
I'm trying to alter the loop so that it checks each article for a featured image and if it finds one it displays that instead of the truncated article and link.
So basically (in logic)
Check for featured image
if there is a featured image display the featured image (and nothing else).
else display a truncated version of the article.
Can anybody help?
Here you go;
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 960,960 )); ?>
<img src="<?php echo $img_src[0]; ?>" alt="My image" />
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
Have a look at the following in the codex for more info;
Wordpress Featured images
Wordpress Excerpts