如果计划使用WP的内置图像自定义字段,请在single-project.php
(假设仅在单个项目页面上调用库):
// this will get all images attached to that specific post
$imagequery = new WP_Query(
array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg\',
\'post_parent\' => $post->ID
));
if ($imagequery->have_posts()) {
// loop to present those images here
}
else { // if no images attached to that post
// fetch all posts from the taxonomy
$catquery = get_posts(
array(
\'post_type\' => \'projects\',
\'numberposts\' => -1,
\'yourtaxonomyname\' => \'yourtermname\' // specify the \'category\' here
));
$postids = array();
if ($catquery) {
foreach ( $catquery as $onepost ) {
array_push($postids, $onepost->ID); // make array of post ids
}
$newimagequery = new WP_Query( // then query these ids to get their images
array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg\',
\'post__in\' => $postids
));
if ($newimagequery->have_posts()) {
// loop to present images from other posts here
}
}