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

ryan champin
ryan champin
16,836 Points

Wordpress Custom Post Types with their own templates?

So i have created a custom post type called "Scroller Section". The idea is, to have a static home page that displays these "Scroller Sections" with the WP loop on the home page. Each scroller section is a fullpage section to display content. Each Scroller Section is technically a post obviously, and when they click on the create Scroller Section in the admin...they should be able to select a template for it (Scroller Section Left or Scroller Section Right). The problem is in the Create Scroller Section theres no choice to choose a template. As of now, in the home page Page section in the admin, i have the main Scroller template selected which is:

<?php 
/**
*Template Name: Scroller
*/
?>
<?php get_header(); ?>

<?php
    $args = array(
    'post_type' => 'scroller section',
    'orderby' => 'date',
    'order' => 'ASC'
);
$query = new WP_Query( $args );
$scrollerSections = 0; //to see if
?>


<div id="fullpage">
    <?php if( $query->have_posts() ) : while( $query-> have_posts() ) : $query->the_post(); $scrollerSections++; ?>
        <section class="section scrollerSection" style="background:<?php    the_field('background_color'); ?>">
            <?php if ($scrollerSections > 1): the_title(); endif; ?>
            <?php the_content(); ?>
            <?php the_post_thumbnail(); ?>
        </section>
    <?php endwhile; endif; wp_reset_postdata(); ?>
</div>
<div id="myMenu" <?php if (get_theme_mod('ryan') == 1) : ?>style="display: none"<?    php endif; ?>></div>


<?php get_footer(); ?>

As you can see...this is just selecting the basic scroller template to display all scroller sections and NOT giving the option for each individual Scroller Section to have ITS OWN TEMPLATE. I am using Custom Post Type UI to create the Scroller Section.

1 Answer

rtprjct
rtprjct
30,548 Points

FIRSTLY

Watch out for the way you call the 'post_type' in your $args array. To be safe, check the slug from the admin area.

For the following example I created a post type called "hey fam" with CTP UI. as you can see the slug is now "heyfam": http://alexartlett.com/wp-content/uploads/2015/06/Screen-Shot-2015-06-29-at-10.10.58-pm.png

Custom Post Type

So I assume you will need to use 'post_type' => 'scrollersection',

I saved your code into a file called page-scroller.php, added a page with that set as its' template and it worked

The changes I did make: I did add get_header(); and get_footer(); to the file so that js/css and header/footer markup can be included AND I had to remove this inline style coz it used an "Advanced Custom Fields" function which I don't have installed.

here is the code, (I think you'll just need to only change 'post_type' value to 'scrollersection' to get it displaying:

<?php 
/**
*Template Name: Scroller
*/
?>


<?php get_header(); ?>

<?php
    $args = array(
    'post_type' => 'heyfam',
    'orderby' => 'date',
    'order' => 'ASC'
);
$query = new WP_Query( $args );
$scrollerSections = 0; //to see if
?>



    <?php if( $query->have_posts() ) : while( $query-> have_posts() ) : $query->the_post(); $scrollerSections++; ?>
        <section class="section scrollerSection" style="background:<?php    the_field('background_color'); ?>">
            <?php if ($scrollerSections > 1): the_title(); endif; ?>
            <?php the_content(); ?>
            <?php the_post_thumbnail(); ?>
        </section>
    <?php endwhile; endif; wp_reset_postdata(); ?>




<?php get_footer(); ?>

SECONDLY If you want the user to choose between "(Scroller Section Left or Scroller Section Right)" I believe that is something you'd have to set in Advanced Custom Fields. Set a drop down menu/or a radio button group so that a user can choose either "Scroller Section Left" or "Scroller Section Right" as a custom field for this 'scrollersection' post type. Then you can do a if statement within your Query loop to programatically determine what to do depending on which option the user has chosen

Sorry if I have not understood your questions 100%