2015-08-08 25 views
0

我想在我的滑動條中顯示特色圖片。所以我在我的function.php做了如下:顯示wordpress滑塊中的特色圖片

<?php 

    function revconcept_get_images($post_id) { 
    global $post; 

    $thumbnail_ID = get_post_thumbnail_id(); 

    $images = get_children(array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 

    if ($images) : 

     foreach ($images as $attachment_id => $image) : 

      $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt 
      if ($img_alt == '') : $img_alt = $image->post_title; endif; 

      $big_array = image_downsize($image->ID, 'large'); 
      $img_url = $big_array[0]; 

      echo '<li>'; 
      echo '<img src="'; 
      echo $img_url; 
      echo '" alt="'; 
      echo $img_alt; 
      echo '" />'; 
      echo '</li><!--end slide-->'; 

    endforeach; endif; } 
    ?> 
      <div class="flexslider"> <!-- function called in index.php --> 
        <ul class="slides"> 
        <?php revconcept_get_images("$post->ID"); ?> 
       </ul> 
      </div><!--end flexslider--> 

現在我有兩個問題。 no.1我的精選圖像沒有顯示在滑塊中。它顯示了一個斷開的鏈接。但它可以回顯post_id。 No.2我想只顯示來自cat = 5的最新帖子的3/4圖像。任何人都可以幫我做到這一點?請注意,我爲我的滑塊使用了flexslider。

+0

您嘗試使用DId:'$ thumbnail_ID = get_post_thumbnail_id($ post_id);'? –

+0

剛剛嘗試..不工作。 – Nazmul

回答

0

好的,這是我測試過的,你可能想要稍微改進一下查詢,但對我來說它工作。在functions.php

function revconcept_get_images($post_id) { 

    $images = get_posts(array(
       'post_type' => 'attachment', 
       'hide_empty' => true, 
       'order' => 'ASC', 
       'orderby' => 'menu_order ID' 
      )); 

    foreach ($images as $image) { 
     $image_url = $image->guid; 
     $img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt 

     echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>'; 

    } 

} 

而在index.php文件放(您想)

<div class="flexslider"> <!-- function called in index.php --> 
    <ul class="slides"> 
     <?php revconcept_get_images($post->ID); ?> 
    </ul> 
</div><!--end flexslider--> 

如果你想顯示全部精選從帖子圖片你還只是把'posts_per_page' => -1,get_posts()陣列英寸

+0

感謝您的支持,您的代碼現在適合我。你能告訴我兩件事,那就是我如何才能在滑塊中顯示category = 4 post的圖像?以及如何使滑塊鏈接中的圖像能夠與相應的帖子相關聯。我很抱歉太多,我是新手在WordPress和PHP。 – Nazmul

+0

如果要過濾來自某個類別的圖像,則需要在查詢中添加'cat'=>#ID',其中'#ID'是要顯示的類別的ID,或者是'' category_name'=>'name''(使用類別名稱)。鏈接它不應該那麼難,因爲你有帖子ID。而不是當前的回聲嘗試'echo'

  • '.$img_alt.'
  • ';' –