admin管理员组文章数量:1122832
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?
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?
Share Improve this question asked Apr 1, 2014 at 15:58 Kent MillerKent Miller 1511 gold badge5 silver badges11 bronze badges 3- 1 see this answer – Milo Commented Apr 1, 2014 at 16:02
- @milo thank you, but there really is no easier method? – Kent Miller Commented Apr 1, 2014 at 16:06
- 4 @KentMiller How much easier do you want it? The code's there, ready to copy-paste. – TheDeadMedic Commented Apr 1, 2014 at 16:25
2 Answers
Reset to default 0I'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.
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
}
本文标签: custom post typesTwo singlephp files
版权声明:本文标题:custom post types - Two single.php files? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293054a1929064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论