Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Custom post types in Wordpress

Hi, I have created a custom post type in Wordpress and now I need those posts to appear on the front end. From my research I need to duplicate the single post php file and also the blog php file.

I do not know php at all. By question is, do I need to learn php before I do this? Is this an easy process or am I better hiring a php developer to do this? I need this to be working in about a week so what is the best thing for me to do next?

Thanks

3 Answers

Greg Harris
Greg Harris
43,362 Points

You don't really need to understand a lot about PHP in order to make this happen. The wordpress template hierarchy does all the magic for you. It sounds like you have already done a little research.

For creating the single pages, it could be as easy as copying single.php and then naming it single-widgets.php if the name of your custom objects was widgets.

If you do end up hiring a developer, don't spend much as this should take someone who is qualified not much more than 15 minutes to fully implement, that includes connecting to your server, downloading the files they need to work with and uploading the new ones.

I hope this is helpful

Thanks Greg for your reply. This is something that I would love to know how to do so appreciate your help.

I am using the Bridge theme so it seems that would make it more difficult, correct? As I have to work within their framework?

The CPT is "costume". The posts are appearing as a single page, no problem. http://b76.e59.myftpupload.com/costume/costume-4/

My goal is to create a costume page - masonry style with a filter. Like this: http://b76.e59.myftpupload.com/costume-hire/

This page is showing the costume categories that I made, but still showing regular posts - not the costume posts.

I attempted to duplicate this page template and I replaced every "blog" word with "costume". but to no avail.

I have contacted the theme devs but they are unwilling to help as it's outside the scope of what they do.

What shall I do next do you think? Do you think this is work for a progammer or can you point me to a course here on Treehouse that you think would help me? I have had a look but not sure which course would teach me this skill.

Once you set up your .php file names you will need to Loop Through the Custom Post Types like below:

<?php

$args = array(
            'post_type'           => 'property', //this is your custom post type
             'posts_per_page' => -1, //this will call all your posts
);

    $loop = new WP_Query( $args ); //new instance of the loop with custom post type in args.

    //THE LOOP
    if ( $loop->have_posts() ) : 
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="pindex">
                <?php if ( has_post_thumbnail() ) { ?>
                    <div class="pimage">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                <?php } ?>
                <div class="ptitle">
                    <h2><?php echo get_the_title(); ?></h2>
                </div>
            </div>
        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            </div>
        <?php endif;
    endif;
    wp_reset_postdata();
?>