Question

I've just figured out how to make custom post types and feed them out onto a page, I am wondering if I can change the single post pages for them?

Can I just set up a single-news.php for example? If so, how would that PHP file be formatted and how does WordPress know to use that single-news.php file? I'm just looking to have them spit out the full article.

Thank you!!

Was it helpful?

Solution

After you created the CPT, do this for showing single posts of your CPT:

  • Duplicate the single.php file in your template and rename it like single-{post_type}.php (eg. single-movie.php)
  • Flush the permalinks from WordPress

You can get more details from this post

  • Now if you want to show a list of CPT you can use get_posts() with args:

    $args = array( ... 'post_type' => 'movie' )

OTHER TIPS

Custom Post Type in wordpress. Basic four steps 

Step 1 : 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 );
}
?>

Step 2: 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; ?>

Step 3: Create new template for show single post like this

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

Step 4: 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.

for this purpose you can create the template file for your single-news.php page. and get your post as you want by wordpress query. for example your single-news.php page

<?php
//Template Name: Single News
?>

<?php
//get your content
$args = array('category' =>'9','posts_per_page'=> 3);
                        $myposts = get_posts( $args );
                        global $post;
                        foreach ( $myposts as $post ) : setup_postdata( $post ); ?> 

//go with your content
Here are some urls u can idea about the creating custom post templates.

https://codex.wordpress.org/Post_Type_Templates
http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
https://codex.wordpress.org/Post_Type_Templates

With the help of above urls u can get idea how you can create new templates for custom post types and custom taxonomy. I think this will help you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top