2016-10-05 20 views
2

我看了一下這篇文章,並複製了一些幫助一點的代碼(頁面不再是完全空白)如何從具有自定義分類法的帖子中抓取精選圖片網址?

我試圖抓住兩個URL:URL到完整大小的圖像和縮略圖版本

從我創建的自定義分類標準中的每個帖子(我已創建分類標準「art-type」,並有3個選項:繪圖,打印和數字)

這是我到目前爲止已經編寫的代碼:

 <?php 
     $args = array(
     'post_type' => 'art-piece', /* custom post type */ 
     'taxonomy' => 'art-type', /* custom taxonomy */ 
     'field' => 'slug', 
     'terms' => array('digital') 
     ); 

     $gallery = get_posts ($args); 
     ?> 


      <div class="zoom-gallery"> 
       <div class="gallery-item"> 

       <?php if (has_post_thumbnail($post->ID)): ?> 
       <?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail'); ?> 

       <img src="<?php echo $url ?>" /> 

       <?php endif; ?>         
       </div> 
      </div> 

從我所看到的,循環中沒有使用我指定的任何$ args。理論上這應該抓住所有的特色圖片,無論是發佈類型,分類等,但他們根本不會爲我而來。該頁面是完全空白的

回答

0

它在我看來,你沒有循環get_posts()調用的結果。嘗試更改代碼以匹配我在下面發佈的更新代碼。

它所做的是通過存儲在$gallery變量中的每個'帖子'循環。

道歉沒有足夠的時間給出更深入的解釋,但是如果這對你有效,我會回來再詳細說明。

<?php 
    $args = array(
     'post_type' => 'art-piece', /* custom post type */ 
     'tax_query' => array(
      array(
       'taxonomy' => 'art-type', /* custom taxonomy */ 
       'field' => 'slug', 
       'terms' => array('digital') 
      ), 
     ), 
    ); 

    $gallery = get_posts($args); // removed space between function name and opening bracket 
    foreach ($gallery as $post) : setup_postdata($post); // added this loop 
    ?> 


     <div class="zoom-gallery"> 
      <div class="gallery-item"> 

      <?php if (has_post_thumbnail($post->ID)): ?> 
      <?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail'); ?> 

      <img src="<?php echo $url ?>" /> 

      <?php endif; ?>         
      </div> 
     </div> 
    <?php endforeach; // ending the loop 
    wp_reset_postdata(); // reset $post so we don't interfere with the main WP_Query 
    ?> 
+0

是的!這工作完美。我添加了另一行,因爲我需要抓取圖像的大版本 我添加了 <?php $ largeurl = wp_get_attachment_url(get_post_thumbnail_id($ post-> ID),'full'); ?> 搶大圖像,而每一件事情是非常 – DipoOgunmodede

+0

我的一個朋友也看準的$ args 我應該寫「tax_query」的錯誤,然後把 「分類」 =>「藝術型',/ * custom customonomy */ 'field'=>'slug', 'terms'=> array('digital') into a array – DipoOgunmodede

相關問題