2012-08-08 34 views
1

我有顯示WordPress的縮略圖簡單的WordPress的if/else

我想要做的是兩種不同的方法,顯示第一種方法,如果它是不可用,則可以顯示使用第二種方法大拇指。

以下是顯示張貼縮略圖的兩種方法。

方法1

<!--Begin WordPress Featured post thumbnail--> 
<div class="postthumbcon"> 
<?php 
// check if the post has a featured image assigned to it. 
if (has_post_thumbnail()) { 
// get the src of the large size featured image 
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large'); 
$thumbnailSrc = $src[0]; 
// output image resized with timthumb 
?> 
<div class="postthumb"> 
"> 
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt=""> 

</div> 
<?php } ?> 
</div> 
<!--end WordPress Featured post thumbnail--> 

這裏是第二方法。

<!--Begin Timthumb thumbnail--> 
<?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?> 

<?php if (get_post_meta($post->ID, 'thumb', true)) { ?> 
<div class="postthumb"> 
" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" /> 
</div> 
<?php } ?> 

<!--End Timthumb thumbnail--> 

回答

0

嘗試這樣:

<div class="postthumbcon"> 
<?php 
if (has_post_thumbnail()) { 
    // method one 
} else if (get_post_meta($post->ID, 'thumb', true)) { 
    // Method two 
} 
?> 
</div> 

這將看看是否有後的縮略圖,你已經在做的,但如果它不存在,它將運行else條款的含義第二個if語句只有在第一個語句失敗時纔會運行。

而且,你似乎有什麼,我猜是在方法二中的鏈接的第一部分,看到這一行:

" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" /> 

這裏是你的代碼如何能期待在全:

<div class="postthumbcon"> 
    <?php 
    if (has_post_thumbnail()) { 
     $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large'); 
     $thumbnailSrc = $src[0]; 
    ?> 
     <div class="postthumb"> 
      <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt="" /> 
     </div> 
    <?php } else if (get_post_meta($post->ID, 'thumb', true)) { ?> 
     <div class="postthumb"> 
      <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> 
       <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" /> 
      </a> 
     </div> 
    <?php } ?> 
</div> 
+0

嗨,謝謝。那完美的工作。 – 2012-08-08 10:08:57

+0

沒問題,你應該接受答案,讓其他人可以看到它的解決:) – 2012-08-08 10:11:48

+0

謝謝,非常感謝。 – 2012-08-08 10:55:35