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

single-{posty-type}.php not working

l have craeted a custom-post as below:

add_action( 'init', 'create_events_type' );
function create_events_type() {
  register_post_type( 'events',
    array(

      'labels' => array(
        'name' => __( 'Events' ),
        'singular_name' => __( 'event' ),
      ),
      'taxonomies' => array('category'), 
      'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
      'public' => true,



      'has_archive' => false,
      'menu_icon' => 'dashicons-schedule'
    )
  );
}

The step above works fine and got a custom-post type called events in my admin area. l however wanted the post in the events to display differently so created a file called single-events.php. and its got the below code in it:

<?php
/**
 * The Template for displaying all single posts
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */
get_header(); ?>
<?php while( have_posts() ) :the_post(); ?> 
     <div class="section-02">

        <aside class="events">
        <h3><?php the_title(); ?></h3>
          <?php the_content();?>
          <?php $value = get_field('button-href');
          if($value):?>
          <div>
           <a href="<?php echo $value;?>" target="_blank"><button>Book Now</button></a>    
          </div>
          <?php endif;?>
        </aside>

        <div class="full-bg"></div>
     </div> 
     <?php endwhile;?>

<?php get_footer(); ?>

however when l try viewing an event post it shows the layout for the default single.php template and not the one l have created. Any ideas how l can resolve this please Zac Gordon

1 Answer

Hi there!

I didn't go through your code line by line, however when WP can not "find" your single-cpt.php then it is most likely because you have not flushed and rebuilt the permalinks after activating this new post type.

Add this line of code to your functions.php after the part where you registered the cpt:

function theme_prefix_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'theme_prefix_rewrite_flush' ); 

After saving the functions.php back to your theme directory (in case you have registered the cpt via your theme functions.php), go to your WP Backend and navigate to:

Settings > Permalinks

and click the "Apply changes" Button at the very bottom of that Permalink area on the right.

This will rebuild your WP Permalink structure and should make WP recognize your single-events.php

Good luck Saskia

Sue Dough
Sue Dough
35,800 Points

I would like to note adding that function is optional. You can save your permalinks and cause it to react without adding this snippet.

Yes, this is true. However it comes in handy, because it automatically causes your permalinks to rebuild, as soon as you switch themes.

l totally agree with you ghost code. l did try it however Saskia Lund but it seem to be passing.