2012-11-15 23 views
1

我目前有 <?php the_post_thumbnail('post-thumbnail', array('class' => "search-thumb attachment-post-thumbnail")); ?>內的一個div,其中包含搜索結果標題和摘錄。我試圖實現的是ONLY如果帖子有可用,則添加縮略圖。僅當縮略圖可用時纔將縮略圖添加到搜索結果中?

我的理由是因爲添加一個縮略圖會使div的高度變大。 讓我們說,縮略圖的div是150px的高度。如果搜索結果中沒有縮略圖,我想消除高度並僅使文本適合文本。 search.php中//

<?php get_header(); ?> 

<div id="container" class="clearfix"> 
<div id="content-wrap"> 

<h1 class="heading1">Search Results</h1> 
<div class="br"></div> 

<div class="clear"></div> 
<h2><?php echo 'Items Found ' . $wp_query->found_posts; ?></h2> 
<div class="clear"></div> 

<?php if(have_posts()): ?> <?php while(have_posts()) : the_post(); ?> 


    <div class="s-res"> 
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> 
    <div class="search-thumb"> 
      <?php the_post_thumbnail('post-thumbnail', array('class' => "search-thumb attachment-post-thumbnail")); ?> 
     </div> 
    <?php echo str_replace('[...]', '', get_the_excerpt()); ?> 
    </div> 

    <?php endwhile; ?> 
    <?php else : ?> 
    <i><?php _e('Sorry, but nothing matched your search criteria.<br> Please try again with different keywords.'); ?></i> 
    <?php endif; ?> 

</div> 
</div> 





<?php get_footer(); ?> 

CSS內

HTML顯示高度ECT //

.s-res { 
    height: 150px; 
    margin-bottom: 10px; 
} 

img.search-thumb { 
    float: left; 
    background: url(images/diag_lines.png) repeat; 
    height: 100px; 
    padding: 10px; 
} 

SCREENSHOT //

screenshot http://i49.tinypic.com/2vv73oh.png

回答

3

只需在if (has_post_thumbnail()) {...條件塊內包裝縮略圖div即可。這裏是你的代碼應該是什麼樣子,當你使用的功能,如:

<?php get_header(); ?> 

<div id="container" class="clearfix"> 
<div id="content-wrap"> 

<h1 class="heading1">Search Results</h1> 
<div class="br"></div> 

<div class="clear"></div> 
<h2><?php echo 'Items Found ' . $wp_query->found_posts; ?></h2> 
<div class="clear"></div> 

<?php if(have_posts()): ?> <?php while(have_posts()) : the_post(); ?> 

    <div class="s-res"> 
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> 
    <?php if (has_post_thumbnail()): ?> 
     <div class="search-thumb"> 
      <?php the_post_thumbnail('post-thumbnail', array('class' => "search-thumb attachment-post-thumbnail")); ?> 
     </div> 
    <?php endif ?> 
    <?php echo str_replace('[...]', '', get_the_excerpt()); ?> 
    </div> 

    <?php endwhile; ?> 
    <?php else : ?> 
    <i><?php _e('Sorry, but nothing matched your search criteria.<br> Please try again with different keywords.'); ?></i> 
    <?php endif; ?> 

</div> 
</div> 

<?php get_footer(); ?> 

我建議你先問一個問題之前,做一個快速的搜索Google;)這個功能是很容易找到,除了WordPress有一個非常好的代碼文檔,所以很有可能你可以在那裏找到你大部分問題的答案。