我對Wordpress相當陌生,我試圖製作一個在媒體類別下加載圖像的功能。媒體類別有一個我想傳入功能的slu g。如果有更簡單的方法來做到這一點,請讓我知道。下面是我到目前爲止的代碼:通過Wordpress中的slu Get獲取圖像
的functions.php
function get_image_by_slug($slug) {
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'attachment_category',
'field' => 'slug',
'terms' => $slug,
),
),
);
$query_images = new WP_Query($query_images_args);
$images = array();
foreach ($query_images->posts as $image) {
$images[]= $image->guid;
}
return $images;
}
function display_image_by_slug() {
$imgs = get_image_by_slug($slug);
$html = '<ul class="list-inline">';
foreach($imgs as $img) {
$html .= '<li><img src="' . $img . '" alt="" /></li>';
}
$html .= '</ul>';
return $html;
}
add_filter('display_slugs','display_image_by_slug');
在頁面
<?php apply_filter('display slugs', 'test_slug');?>
但是,我不只是檢索一個圖像,而是我想要檢索所有具有slu images的圖像。我有一個媒體類別的'測試'slu 3下的3張圖片。 – Elli
更改'posts_per_page = -1',它將獲取您擁有的媒體分類中的所有可用數據。 –
@Elli。如果是這樣,在應用這個之後,您將面臨錯誤分享的想法,我們將討論您的策略以獲取圖像。 –