0

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?

1 Answer 1

1

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
}


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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