admin管理员组

文章数量:1122832

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>'; ?>

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>'; ?>
Share Improve this question edited Jan 30, 2013 at 2:02 asked Jan 30, 2013 at 0:00 user24259user24259 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
Add a comment  | 

2 Answers 2

Reset to default 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

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>

本文标签: postsInsert link to my thumbnail images