2016-11-25 122 views
1

似乎有幾個類似問題的答案,但我還沒有找到一個適合我的工作。從分類圖庫中獲取圖像

我有一個稱爲entertainement的自定義後期類型。 entertainement有一個分類標準ent_categories

其中ent_categories被稱爲Event

每個Event有一個畫廊,我試圖做一個查詢,將返回已被添加到任何CPT entertainmentEvent類別最新的10張圖像。

我希望能得到一個數組中的URL列表。

從我讀到這裏這樣的事應該做​​的伎倆:

$arg = array(
    'post_status' => 'inherit', 
     'posts_per_page' => -1, 
     'post_type' => 'attachment', 
); 


$arg['tax_query'] = array(
array(
    'taxonomy' => 'ent_categories', 
    'field' => 'name', 
    'terms' => array('Event'), 

), 
); 

$the_query = new WP_Query($arg); 
var_dump($the_query); 

var_dump($the_query);顯示了很多的東西,但沒有圖像? 關於這個的任何提示? 謝謝

編輯:

我剛纔看到了,我可以這樣做:在所有的畫廊圖片的網址都會顯示在畫廊下面的圖片

function pw_show_gallery_image_urls($content) { 
    global $post; 
    // Only do this on singular items 
    if(! is_singular()) 
     return $content; 
    // Make sure the post has a gallery in it 
    if(! has_shortcode($post->post_content, 'gallery')) 
     return $content; 
    // Retrieve all galleries of this post 
    $galleries = get_post_galleries_images($post); 
    $image_list = '<ul>'; 
    // Loop through all galleries found 
    foreach($galleries as $gallery) { 
     // Loop through each image in each gallery 
     foreach($gallery as $image) { 
      $image_list .= '<li>' . $image . '</li>'; 
     } 
    } 
    $image_list .= '</ul>'; 
    // Append our image list to the content of our post 
    $content .= $image_list; 
    return $content; 
} 
add_filter('the_content', 'pw_show_gallery_image_urls'); 

這個結果。 也許這個功能可以從頁面而不是從functions.php中調用?

+0

你是什麼意思_Each'Event'有一個gallery_?你的意思是ACF的畫廊領域? –

+0

謝謝你的回答。不,不是ACF galleryfield。使用「標準」wordpress圖庫作爲簡碼添加圖庫 – eggman

回答

1

你在正確的軌道上,但你與ent_categories分類,只適用於entertainement職位的term查詢attachment類型的職位,所以不會有任何人的,你去看看你:

var_dump($the_query->posts); 

如果轉儲全部$the_query你會看到很多事情因爲它是一個WP_Query對象。

您需要查詢您的entertainement帖子:(要小心,因爲你在塞有一個錯字!)

$arg = array(
    'posts_per_page' => -1, 
    'post_type'  => 'entertainement', 
); 


$arg['tax_query'] = array(
    array(
     'taxonomy' => 'ent_categories', 
     'field' => 'name', 
     'terms' => 'Event', 

    ), 
); 

$the_query = new WP_Query($arg); 

然後你可以遍歷的職位,並得到這樣的庫項目:

if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     if (get_post_gallery()) : 
      echo get_post_gallery(); 
      print_r(get_post_gallery_images()); 
     endif; 
    } 
    /* Restore original Post Data */ 
    wp_reset_postdata(); 
} 

get_post_gallery_images()將讓你的URL的畫廊圖像陣列

get_post_gallery()會讓你真正的HTML打印畫廊。

+0

非常感謝您提供有效的解決方案和一個很好的解釋! – eggman