Create a ‘single’ page for a Custom Post Type

Ok, I installed the Custom Post Type UI plugin and create one. I then added a new post to it. In my theme, i have a piece of code like this:

<?php 
    $loop = new WP_Query( array( 
        'post_type' => 'case studies',   /* edit this line */
        'posts_per_page' => 15 ) );
?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>  

    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail('thumbnail'); ?>
    </a>

<?php endwhile; ?> 

Now, firstly if i click the thumbnail, i get an error in the browser saying it’s in a redirect loop, but secondly I’d like to know exactly what files i need to create to view a single post of this custom post type. And what to put in that file.

Related posts

Leave a Reply

4 comments

  1. Use single-{posttype}.php for the single template. Also, if you register your post type with the has_archive argument set to true, then you can use archive-{posttype}.php for your archive template, which will allow you to skip that query that you have there, since the global $wp_query object will already be populated with your custom post type.

    BTW, you have a space in your post_type argument, which will be a problem.

    Check out the Template Hierarchy, and consider registering your CPTs using code in a plugin rather than using a CPT UI plugin.

  2. There’s no need as WordPress will use the default page template however you can create a custom single-cpt.php file where cpt is the name of your registered post type.

    <?php
    
    get_header(); ?>
    
    <div id="main-content" class="main-content">
    
        <div id="primary" class="content-area">
            <div id="content" class="site-content" role="main">
                <?php
                    // Start the Loop.
                    while ( have_posts() ) : the_post();
    
                        // Include the page content template.
                        get_template_part( 'content', 'page' );
    
                    endwhile;
                ?>
            </div><!-- #content -->
        </div><!-- #primary -->
    </div><!-- #main-content -->
    
    <?php
    get_sidebar();
    get_footer();
    
  3. You could just write this into your single.php file (within the loop) and echo out whatever fields you need within the if statement.

    if($post_type == 'case_studies') { // you may need this to be without spaces (machine name)
    
                    echo '<h1>'.get_the_title().' flavors</h1>';
    
                   // post id
                 $post_id = get_the_ID();
                  get_post_meta($post_id, 'custom_field_name', true);
    
    
                    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
                      <?php endwhile; ?>
    
     }
    

    Another option is t0 create a page template. Copy your single.php file and rename it case_studies.php .. at the top within php tags add:

    <?php
    /*
    Template Name: Brand Output 04/12
    */
    ?>
    

    and then add the same if statement within the single.php loop as the above example…

  4. Custom Post Type in wordpress.Basic four steps.Step1: File Path location : theme/function.php in your theme.Paste code in function.php (register custom post type )

    <?php
    
    add_action( 'init', 'custom_post_type_func' );
    function custom_post_type_func() {
        //posttypename = services
    $labels = array(
    'name' => _x( 'Services', 'services' ),
    'singular_name' => _x( 'services', 'services' ),
    'add_new' => _x( 'Add New', 'services' ),
    'add_new_item' => _x( 'Add New services', 'services' ),
    'edit_item' => _x( 'Edit services', 'services' ),
    'new_item' => _x( 'New services', 'services' ),
    'view_item' => _x( 'View services', 'services' ),
    'search_items' => _x( 'Search services', 'services' ),
    'not_found' => _x( 'No services found', 'services' ),
    'not_found_in_trash' => _x( 'No services found in Trash', 'services' ),
    'parent_item_colon' => _x( 'Parent services:', 'services' ),
    'menu_name' => _x( 'Services', 'services' ),
    );
    $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Hi, this is my custom post type.',
    'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
    'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
    );
    register_post_type( 'services', $args );
    }
    ?>
    

    Step2: how can show wordpress custom post type in wordpress template page ?

    You can show anywhere in template page like this :

    <?php   $args = array( 'post_type' => 'services', 'posts_per_page' => 20 );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <div class="services-items">
                <?php the_title(); 
            if ( has_post_thumbnail( $post->ID ) ) {
            echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
            echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
            echo '</a>'; }
    
    ?>
                </div>
        <?php endwhile; ?>
    

    Step3: Create new template for show single post like this

    single-{custom post type name}.php
    or
    single-services.php

    Step4: Paste code in single-services.php file

     <?php /* The loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>
                    <div class="main-post-div">
                    <div class="single-page-post-heading">
                    <h1><?php the_title(); ?></h1>
                    </div>
                    <div class="content-here">
                    <?php  the_content();  ?>
                    </div>
                    <div class="comment-section-here"
                    <?php //comments_template(); ?>
                    </div>
                    </div>
    
                <?php endwhile; ?>
    

    This is custom post type example with single post page.