3

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 :)

1
  • Please show us what you have tried so far, such as your code.
    – GhostToast
    Commented Aug 23, 2013 at 13:12

2 Answers 2

2

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.

8
  • 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 Commented Aug 24, 2013 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)? Commented Aug 25, 2013 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. Commented Aug 26, 2013 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! Commented Aug 26, 2013 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. Commented Aug 26, 2013 at 14:23
0
<?php 
if (have_posts()) : 
    while (have_posts()) : the_post();   
        if ( has_post_thumbnail($post->ID) ) {
            the_post_thumbnail($post->ID);
        } 
    endwhile; 
endif;
?>
1
  • Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Aug 23, 2013 at 13:10

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.