Thumbnail or Preview in the Wordpress Loop - php

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

Related

prettyPhoto on a Wordpress featured image

I am pretty new at this and tried to search a lot about this, breaking my site down numerous times because of wrong PHP code, so here is the deal.
I have a Wordpress page with portfolio posts. In those posts the only image displayed is the featured image. Due to that my prettyPhoto plugin won't show the lightbox when I click on that featured photo.
First I had this code ...
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
... which showed the featured image but didn't show the lightbox so it was not clickable. Then I played around and created this:
<?php if ( has_post_thumbnail()) : ?>
<a class="lightbox_single_portfolio" title="<?php echo esc_attr($title); ?>"
href="<?php echo esc_url($image_src); ?>" data-
rel="prettyPhoto[single_pretty_photo]">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
So the featured image got clickable, the lightbox started opening but there is an error that states "Image cannot be loaded. Make sure the path is correct and image exists". Due to that I said to myself that the problem could be that lightbox isn't loading the right image source.
I tried playing a bit more and tried to do this ...
<?php
if ( has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
echo '<a class="lightbox_single_portfolio" href="'.esc_url($image_src).'" data-rel="prettyPhoto[single_pretty_photo]">';
the_post_thumbnail('thumbnail');
echo '</a>';
endif;?>
... aaaand it breaks my site.
What could be the problem?
You are not closing the If statement,
change endif; to }
Well, I'm gonna post an answer as I found out something on the internet and it made my code work. Although the above answer (change endif; to }) did make my code work, it still showed the same error that the path couldn't be found.
I played around again a little bit, and this did the trick:
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a rel="prettyPhoto" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail();
echo '</a>';
}
?>
So the PHP calls a $large_image_url instead of the post thumbnail url.
This did the trick, and it works! It shows my featured image in a lightbox :)

issue in displaying blog image on blog page in wordpress

i am having a blog page in my website... There are different posts ... on left side blog image is displayed , on right side text is written..when i click on particular blog, the main page of that post appears with image on the top and text down the image.
The issue is that on my blog page the left side images are not displayed rather default image is displayed.. bt when i click on post.. the main page of that particular post is having image...
<div class="blog-img mainimg" style="">
<?php $blogmainimg = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );?>
<?php if($blogmainimg[0] == '') : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php else : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
</div>
only "if" is working.... else is not working
on blog page, all the posts are having NOIMG.png.. which is wrong
???
Let's try a more simplified solution. This uses the has_post_thumbnail() to check if a featured image is specified, since you really do not need to utilize the wp_get_attachment_image_src() in the provided code.
<div class="blog-img mainimg" style="">
<?php if(has_post_thumbnail($post->ID)) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php endif; ?>
</div>
This uses has_post_thumbnail() to see if the post has one specified, if it does, it will display it uses the_post_thumbnail(). If not, it will revert to the default.
If this doesn't work, than it could be an issue of using the_post_thumbnail(), this would mostly depend on your loop that's displaying the blog posts. If that is the case, try this:
<div class="blog-img mainimg" style="">
<?php if(has_post_thumbnail($post->ID)) : ?>
<?php echo get_the_post_thumbnail( $post->ID, 'single-post-thumbnail' ); ?>
<?php else : ?>
<img src="<?php bloginfo('template_url')?>/images/Noimg.png" />
<?php endif; ?>
</div>
If this still does not solve the issue, please post the loop for your page as well, and I can further troubleshoot it.

If my WordPress post does not contain a featured image, the caption still appears but it contains the post excerpt

The news articles on my site contain a featured image which has a caption attached.
My problem is that when an article does not contain a featured image the caption still appears, and it contains the excerpt of the news article.
<div class="single-img-container">
<?php
the_post_thumbnail('full', array('class'=>"img-responsive single-news-img"));
echo '<div class="news-img-caption">'.get_post(get_post_thumbnail_id())->post_excerpt.'</div>';
?>
</div>
My ideal result would be that if no featured image is included, the caption div will not appear.
you'll need to use the 'has_post_thumbnail()' conditional to make sure the post/page has a featured image. the code below will output the markup '...' only if a featured image exists.
<?php if ( has_post_thumbnail() ) : ?>
<div class="single-img-container">
<?php
the_post_thumbnail('full', array('class'=>"img-responsive single-news-img"));
echo '<div class="news-img-caption">'.get_post(get_post_thumbnail_id())->post_excerpt.'</div>';
?>
</div>
<?php endif; ?>
reference: http://codex.wordpress.org/Function_Reference/has_post_thumbnail

How to Create Nextgen Custom gallery with album name

i am trying so long to making one normaly gallery useing by Nextgen gallery. but i can't make it yet. i found some way to show gallery thumbnail for speciy gallery and show album name but can't make that dynamicly and show together. bellow some code may be help
for show custom template file.
<?php
/*
Template Name: Gallery
*/
get_header(); ?>
<?php
$NextG = new NextGEN_Shortcodes;
echo $NextG->show_gallery( array("id"=>2,"template"=>"myshow") );
?>
<?php the_post_thumbnail(); ?>
<?php
if ( have_posts() ) while ( have_posts() )
{
the_post();
the_content();
}
?>
<?php get_footer(); ?>
for make custom nextgen gallery file
gallery-mytemplate.php
<ul id="slideshow">
<!-- Thumbnails -->
<?php foreach ( $images as $image ) : ?>
<li>
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
<?php } ?>
</a>
</li>
<?php endforeach; ?>
</ul>
what i want ?
actuly i want my gallery page will have all gallery name top always and if i click any gallery name then show that gallery. see http://www.evokephotoandvideo.com/2013-photo-gallery/ this url something like that. if can be possible also ajax load like ref. line then would be more nicer...
Simplest way for retrieving data from NextGen is to use NextGEN Gallery Database Class stored in ngg-db.php file. Open that file and there are all functions and explanations for taking the data directly from the database.
Example:
$NextG = new nggdb();
// get all galleries
$NextG->find_all_galleries();
// get single gallery
$NextG->get_gallery( 2 );
Results are arrays, to examine them use print_r or print_r2:
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
That way you can take id or slug from all galleries and create custom url's like http://www.evokephotoandvideo.com/gallery/slug.
Additional info:
WP Rewrite

WordPress title image

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 }
} ?>

Categories