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 am new to Wordpress and am busy using it to create an online portfolio site. I have created a custom post type for my portfolio items called 'Portfolio', and I have attached a featured image to each custom post.

For my home page I would like a gallery of the featured images from these custom post types, and if you click on a thumbnail you are taken to that custom post.

I have tried making a template page following instructions from the internet but nothing has worked for me so far and I'm not sure where to start! Coding and Wordpress is all new to me.

If you have any suggestions on how I can do this, please let me know. Thank you :)

share|improve this question
    
Please show us what you have tried so far, such as your code. –  GhostToast Aug 23 '13 at 13:12
add comment

2 Answers

up vote 0 down vote accepted

To create the gallery you want, follow these steps:

1) Navigate to your theme folder and create a file named cpt-gallery.php

2) Copy and paste this code into the file

<?php 
    /*
    Template Name: Custom Post Type Gallery
    Description: Creates a gallery of featured images from a custom post type
    Notes: Make sure you have support for thumbnails enabled
    */

    get_header(); 

    // Query the custom post type to display
    $args = array('post_type' => 'CUSTOM POST TYPE');
    $query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php if ( has_post_thumbnail() ): ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    <?php endif; ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>

3) In WordPress, create a new page named "Home" and on the right side under "Page Attributes" select the template "Custom Post Type Gallery" from the menu.

4) Navigate to Settings > Reading and under the option "Front page displays," select "A static page" and then select the page you created labeled "Home." Save those changes.

NOTES: Make sure that you replace the word "CUSTOM POST TYPE" with the actual name of your custom post type. This code will pull in each custom post type you've defined, loop through them and then display a thumbnail of the featured image for that post with a link to it.

I've tested this so it should work for you just fine. Let me know if you need further clarification.

share|improve this answer
    
Thanks so much for your advice, but when I use this page template my Wordpress crashes with the following error messages: Warning: Cannot modify header information - headers already sent by (output started at /home/kinkefny/public_html/wp-content/themes/kinkedtheme/functions.php:5) in /home/kinkefny/public_html/wp-admin/post.php on line 222 Warning: Cannot modify header information - headers already sent by (output started at /home/kinkefny/public_html/wp-content/themes/kinkedtheme/functions.php:5) in /home/kinkefny/public_html/wp-includes/pluggable.php on line 875 –  Kerry O'Connor Aug 24 '13 at 10:53
    
No problem at all. Could you post what code you have on line 5 (and any surrounding lines if it's apart of a function)? –  WordPress Plugineer Aug 25 '13 at 4:19
    
I fixed that error, I just had to put all the code into one <?php ?> tag and now I'm not getting errors anymore but... Any pages I create are not using the template I select but are displaying as my latest blog posts. So I can't see if the template you made for me is working. I can't use any template files! I have no idea what I have done to cause this problem. –  Kerry O'Connor Aug 26 '13 at 9:06
    
The gallery works if I set it to my homepage! I'd like to have it on a different page, but all of my pages show my latest blog posts regardless of the template I select and I cannot figure out why. Thanks so much for your help so far! –  Kerry O'Connor Aug 26 '13 at 13:26
    
Great! Glad to hear that. To set this template to another page, repeat step #3 above. Make sure you are creating a page and not a post. If you're not aware, only pages will show you the "Page Attributes" menu and allow you to assign this template. So you can create a new page, call it "Gallery" and assign this template to it and it will work just like it is for your homepage. Let me know how it goes. –  WordPress Plugineer Aug 26 '13 at 14:23
show 3 more comments
<?php 
if (have_posts()) : 
    while (have_posts()) : the_post();   
        if ( has_post_thumbnail($post->ID) ) {
            the_post_thumbnail($post->ID);
        } 
    endwhile; 
endif;
?>
share|improve this answer
    
Please add an explanation to your answer: why could that solve the problem? –  toscho Aug 23 '13 at 13:10
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.