Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have several "categories" and so called "projects" (post of content type "project"). To each project corresponds a category.

What I call a "gallery", is several images, with each image attached several strings to (description, author).

To each category and to each project should be attached a gallery. If to a project a gallery isn't attached, on the project page the gallery of the category should be shown.

(Note that what I call here "categories" may or may not be the same as what is called "categories" in the manuals of WordPress.)

How to do it in WordPress?

share|improve this question
add comment

1 Answer

If you plan on using WP's built-in image custom fields, include this query on your single-project.php (assuming galleries will only be called on single project pages):

// 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
}


}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.