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

Ben Taylor
Ben Taylor
902 Points

Featured Custom Post Type?

Hey guys,

I've setup custom post types using Custom Post Type UI and Advanced Custom Fields.

Basically it's for my portfolio.. so I have several post types, web, print and brand. Now they're all working great, but I'd like to have featured projects on my front page.

My question is, is there a way I can add a field to my post types, like a tickbox, which if ticked will display them on the homepage?

Any help would be MUCH appreciated!

Thanks,

Ben

4 Answers

Ben Taylor
Ben Taylor
902 Points

You sir, are a legend, thank you!

Yeah!

Use the custom fields and add a new field, Change the field type to true or false. I guess you need this for every post type!

I understand that your front page is a mash-up of all post types right? I am not sure how you would shuffle them, but you could have one before the other in whatever order you would like.

You would call the field name of the boolean checkbox, in your loop with a conditional statement

Here is an example with no syntax.

> WP Loop web posts

if (the_field(display_on_home) == TRUE) {

 the fields

} else {

 nothing, 

}

> WP Loop "print"

if (the_field(display_on_home) == TRUE) {

 the title the blah blah the the experpt

} else {

 nothing, 

}

This should get you started and you will probably figure out the best way about it !

You could add an argument to the loop and define posts_per_page as 1 for each post type. This would give you the most recent project per type on the front page if you had three loops.

Ben Taylor
Ben Taylor
902 Points

I only want 6 projects showing up on the front page... so basically I would only make sure 6 at any time were ticked... so would your above method work? :) my current code (as per the treehouse course) is:

<?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


                    <div class="entry-content">
                        <?php the_content(); ?>                     
                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                    </div><!-- .entry-content -->


                </article><!-- #post -->

                <?php //comments_template(); ?>
            <?php endwhile; ?>

            <?php 
                $args = array(
                    'post_type' => 'home',
                    'orderby' => 'date',
                    'order' => 'DESC'
                );
                $the_query = new WP_Query( $args );         
            ?>
            <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

            <?php get_template_part( 'content', 'home' ); ?>

            <?php endwhile; endif; ?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

I would suggest placing the IF/ELSE login in your content-home.php file.

Ben Taylor
Ben Taylor
902 Points

Do you have any example code you could show me? :)