2014-05-06 24 views
0

我一直在使用下面的代碼來獲取所有的特色圖像的子頁面,然後顯示在旋轉木馬滑塊,但我想要特定的大小= 231 * 130和class = img-responsive。 但我不能夠做到這一點在wordpress中獲取具體的圖像大小

<?php 
$mypages = get_pages(array('child_of' => 67, 'sort_column' => 'post_date', 'sort_order' => 'desc')); 

foreach($mypages as $page) {  
    $content = $page->post_content; 
    if (! $content) // Check for empty page 
     continue; ?>          

    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"> 
     <div class="thumbnail"> 
      <a href="<?php echo get_page_link($page->ID); ?>"> 
       <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($page->ID, array(231, 130), array('class' => 'img-responsive img-shadow'))); ?> 
       <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>"> 
      </a>  

      <div class="caption clearfix"> 
       <h4><?php echo $page->post_title; ?></h4> 
       <a href="<?php echo get_page_link($page->ID); ?>">view all</a> 
      </div> 
     </div> 
    </div> 
<?php } ?> 

請幫我找出我在做什麼錯

回答

1

使用wp_get_attachment_imageCODEX

你的情況:

<?php 
$mypages = get_pages(array('child_of' => 67, 'sort_column' => 'post_date', 'sort_order' => 'desc')); 

foreach($mypages as $page) {  
    $content = $page->post_content; 
    if (! $content) // Check for empty page 
     continue; ?>          

    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"> 
     <div class="thumbnail"> 
      <a href="<?php echo get_page_link($page->ID); ?>"> 
       <?php $image = wp_get_attachment_image(get_post_thumbnail_id($post->ID), 'child-page','', array('class' => "img-responsive img-shadow", 'alt' => get_the_title())) ?> 
       <?php echo $image; ?> 
      </a>  

      <div class="caption clearfix"> 
       <h4><?php echo $page->post_title; ?></h4> 
       <a href="<?php echo get_page_link($page->ID); ?>">view all</a> 
      </div> 
     </div> 
    </div> 
<?php } ?> 

,並在您的functions.php

add_image_size('child-page', 231, 130, true); 
+0

以上標記非常感謝 – terminator

0

你在你的論點在這裏混合了起來:

<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($page->ID ,array(231,130),array('class'=>'img-responsive img-shadow')));?> 

get_post_thumbnail_id只需要一頁面ID。實際上,你應該做的是嵌套兩個功能。使用這個:

<?php get_the_post_thumbnail($page->ID, array(231,130), array('class' => 'img-responsive img-shadow')); ?> 

這也會回顯結果,所以你可以刪除第二行。該功能的Here's the Codex entry

+0

謝謝我的話d it,但現在我根本不顯示img – terminator

0

試試這可能會幫助您: -

<a href="<?php echo get_page_link($page->ID); ?>"> 
    <?php echo get_the_post_thumbnail($page->ID, array(231,130), array('class' => 'img-responsive img-shadow'));?> 
</a> 
+0

謝謝我嘗試過,但現在我根本不顯示img – terminator

+0

請參閱我的更新回答。您需要爲此功能添加'echo'。 –

+0

我試過了,但它沒有給出我想要的正確圖像尺寸,謝謝任何方式,我已經標記了正確答案 – terminator