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

Konrad Pilch
Konrad Pilch
2,435 Points

Costome Post Type

HI,

So heres what im trying to do :

I have a navigation menu, with one menu thats called services. The menu 'services' has submenus such as 'business consulting', 'tax consulting', 'digital marketing' and so on..

I need to make them a specific template so they all look the same. It's like using page.php but using single.php, i managed to do like most of it? well, heres what i did:

Functions.php

// Services CPT UI
function create_posttype() {

    register_post_type( 'services',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Services' ),
                'singular_name' => __( 'service' )

            ),
            'public' => true,
            'has_archive' => true,
        )
    );
}

single-services.php

        <!-- SERVICE CONTENT -->
        <div class="lg-col-8 col-md-8 col-xs-12">
          <div class="row">
            <div class="col-xs-12">

                <?php
                   $args = array(
                     'post_type'  => 'services',
                     'posts_per_page'     => 1
                   );
                   $the_query = new WP_Query( $args );
                ?>

               <!-- Loop -->
              <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <article class="white-bg">

                <img class="image-thumbnail" src="<?php echo get_field( 'service_image'); ?>">

                <div class="service-content-wrapper border-rd">
                     <h1 class="text-heading"><?php the_title(); ?></h1>
                     <p class="text-muted">
                        <?php the_content(); ?>
                     </p>
                  </div>
              </article>
              <?php endwhile; else : ?>

                <h1>Oh no!</h1>
                <p>No content is appearing for this page!</p>

              <?php endif ?>

            </div><!-- col-xs-12 -->
          </div><!-- row -->
        </div><!-- /col-* -->
        <!-- /SERVICE CONTENT -->

So whats happening now is that when i click on my ACF, and i added three posts, and they all display the template, with the code, BUT, they all display the same post. If i add a new post, it will display it.

It will be like /services/business-consulting.php and it will display me a post that i recently added, if i go to /services/digital-marketing.php it will go to post that i recently added, which is the same display.

What I need to do?

1 Answer

If you are using pages there's really no need to create a custom post type, instead I would create a custom page template, maybe something like page-services.php and then assign that template to each page under your Services. At the very top of your template file you just would need to add:

/**
 * Template Name: Services Template
 */

Where "Services Template" could be anything you wanted. I don't think there's any reason for creating a custom post type for what you are attempting to do, I think a custom page template would be more effective.

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you for your answer.

Well, i don't want to use page.php because thats not best practice, but i did it once and i know it had the effect i wanted. So i discovered single.php and stuff..

I didn't want to create the Template Name because i need to know how to create single.php as im learning about it, so i need to cover it, and since i have pages that would do the same thing as page.php, ill need to do it that way.

I need the saem effects of services and events. So i cant duplicate page.php twice, so i need single.php, which is what i did, but they all display the latest post of the categpory. So e.g. events = Cricket , footbal, Services = tax and money, so it will display me cricket and money, for the event and then for the service section.

Also, when there are going to be lots of pages like services, events, videos and stuff.. there will be hell a lot of Template name names which i think it's alos better to make it automatic than to worry about the template, and especially if a customer is going to use it.

But again, i need to learn it how to do it as im learning. I just need that one thing to finish the site : p

Konrad Pilch
Konrad Pilch
2,435 Points

Plus i can't display a or asign a Template To my Custome Post Types for events that are not pages :/