0

I have a custom post type "people". For each person, I can enter the following custom fields: "address", "occupation" and "pictures".

In the single-people.php I display the address and the occupation. In addition, this single template contains a gallery button - when the website user clicks on it I want to takes the user to a separate page where only the pictures of the person he just looked at are displayed.

How do I achieve that? I cannot just create a second single.php file, right?

3
  • 1
    see this answer
    – Milo
    Apr 1 '14 at 16:02
  • @milo thank you, but there really is no easier method? Apr 1 '14 at 16:06
  • 4
    @KentMiller How much easier do you want it? The code's there, ready to copy-paste. Apr 1 '14 at 16:25
0

I'm a fan of page templates - Here's what I would do.

First create a page called something like Person Gallery, call it whatever you'd like.

Then we can create either a separate template page-person-gallery.php or open our page.php and run a conditional in our loop to test if we're viewing the Person Gallery Page:

<?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post(); ?>
        <?php if($post->post_name == 'person-gallery') : /** Better to test for ID **/ ?>
            <!-- We're on the Person Gallery Page -->
        <?php else : ?>
            <!-- Run default info -->
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

Either method you pick, we can now build a query based off what we know about about the person, which is nothing right now. We will need to build a link on your single.php that can give us a bit more information about the person. So when you create your links to the person-gallery we can pass our person ID. It'll look something like this:

<!-- Again, better getting permalink by ID -->
<a href="<?php echo get_permalink( get_page_by_path( 'person-gallery' ) ).'?person='.$post->ID ?>">View Gallery</a>

Now when somebody clicks that link, it will pass us that person ID which we can then base our query on. So let's create get our images:

<?php
    if(isset($_GET['person']) && is_numeric($_GET['person'])) {
        $pesonID = $_GET['person'];
        $attachments = get_children(
            array(
                'numberposts' => -1,
                'post_mime_type' => 'image',
                'post_parent' => $personID,
                'post_status' => 'any',
                'post_type' => 'attachment'
            )
        );

        /** Now we can display our images! **/
        $attachments = get_children(
            array(
                'numberposts' => -1,
                'post_mime_type' => 'image',
                'post_parent' => $post->ID,
                'post_status' => 'any',
                'post_type' => 'attachment'
            )
        );

        /** Now we can display our images! **/
        foreach($attachments as $imgID => $img){
            echo wp_get_attachment_image($imgID);
        }
    }
    else{
        echo "<p>No Images Found.</p>";
    }
?>

You can pass some sizes and such to wp_get_attachment_image, View The Codex to see what gets returned.

0

You might possibly use the same single-page.php. Being header,footer and conditional sidebar same and just the different middle content.

if(is_singular( 'custom_post_title' ) ){
     // do your conditional stuff
}else if(is_singular( 'custom_post_title_2' )){
     // do your conditional stuff 2 
}else{
    //do other else stuff
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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