0

I am try to insert link to my thumbnail images, the link is hyperlink with post. When i put this code inside the loop then instead of showing the first image, first show the second image (template_url)

<a href="<?php the_permalink() ?>" class="alignnone" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_post_thumbnail(); ?></a>
<?php else : ?>
<img src="<?php bloginfo('template_url'); ?>/images/noimage.jpg" width="213" height="187" alt=""/>
<?php endif; ?>
</a>

The code of the tempate :

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>=">
    <div class="item">  
        <ul>
        <li>
     <a href="<?php the_permalink() ?>" class="alignnone" title="<?php the_title(); ?>">

<a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                                                <?php if (has_post_thumbnail()) : ?>
                                                    <?php the_post_thumbnail(); ?></a>
                                                    <?php else : ?>
                                                        <img src="<?php bloginfo('template_url'); ?>/images/noimage.jpg" width="213" height="187" alt=""/>
                                                    <?php endif; ?>
    </a>                             
    <div id="info">
        <div class="title_album"> <strong>Άlbum : </strong> <?php print_custom_field('title_album'); ?><br /> </div>
                                        <div class="number_photos"><strong>Photos : </strong> <?php print_custom_field('Number_photos'); ?><br /> </div>
                        </div>
                </li>
            </ul>
        </div> <!-- end item -->
    </div> <!-- END div class post -->

Even if this part of code, the result is the same..

<?php   echo '<a href="', get_permalink(), '">';
        if (has_post_thumbnail()) 
{the_post_thumbnail();}
else {echo'<img src="', get_bloginfo('template_directory'), '/images/noimage.jpg',                                          '" width="213" height="187" alt="thumbnail" />';}
        echo '</a>'; ?>
1
  • please post the full code of the template. if there is a featured image, then your code is using two html link tags.
    – Michael
    Commented Jan 30, 2013 at 0:19

2 Answers 2

0

You may want to change your code to something like this:

<a href="<?php the_permalink() ?>" class="alignnone" title="<?php the_title(); ?>">

<a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
    <?php if (has_post_thumbnail()) : ?>
        <?php the_post_thumbnail(); ?></a>
    <?php else : ?>
        <img src="<?php bloginfo('template_url'); ?>/images/noimage.jpg" width="213" height="187" alt=""/>
    <?php endif; ?>
</a>

and I assume you've specified a Featured Image for the post thumbnail to show up yes? http://codex.wordpress.org/Post_Thumbnails

6
  • and what about the double html link tag?
    – Michael
    Commented Jan 30, 2013 at 0:20
  • Thanks for the answer and the help. Michael, there is no featured image, i edit my post and i put the code. @WP Themes this part of code doesn't work.
    – user24259
    Commented Jan 30, 2013 at 0:32
  • @Michael, you have right. I didn't enable the Featured Image. Now works. But now i want to exclude the Featured Image (post_thumbnail) from single-portfolio.php. When hyperlink with post and redirect to single-portfolio.php.i don't want to show the post_thumbnail. How can i do that?
    – user24259
    Commented Jan 30, 2013 at 15:14
  • @MikeD - possibly start a new question; and post the code of single-portfolio.php
    – Michael
    Commented Jan 30, 2013 at 17:37
  • Hi @MikeD, seems like this part of the question has been solved using the code I suggested. I'd appreciate it if you can accept my answer. Thanks! and oh, may want to post a link to your next question.
    – WP Themes
    Commented Jan 31, 2013 at 0:33
0

You could not use anchor tag inside an another anchor tag, Try like this....

<a href="<?php the_permalink() ?>" class="alignnone" title="<?php the_title(); ?>">
    <?php if (has_post_thumbnail()) : ?>
        <?php the_post_thumbnail(); ?>
    <?php else : ?>
        <img src="<?php bloginfo('template_url'); ?>/images/noimage.jpg" width="213" height="187" alt=""/>
    <?php endif; ?>
</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.