I am attempting to create a Wordpress theme that calls for a bespoke Post Type.
I have created the post type via functions.php. This seems to have worked and shows up in the admin area and allows me to generate posts. Code exerpt below:
// Register Custom Post Type
function courses_post_type() {
$labels = array(
'name' => _x( 'Courses', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Course', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Training Courses', 'text_domain' ),
'name_admin_bar' => __( 'Course Type', 'text_domain' ),
'archives' => __( 'Course Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Course:', 'text_domain' ),
'all_items' => __( 'All Courses', 'text_domain' ),
'add_new_item' => __( 'Add New Course', 'text_domain' ),
'add_new' => __( 'New Course', 'text_domain' ),
'new_item' => __( 'New Course', 'text_domain' ),
'edit_item' => __( 'Edit Course', 'text_domain' ),
'update_item' => __( 'Update Course', 'text_domain' ),
'view_item' => __( 'View Course', 'text_domain' ),
'search_items' => __( 'Search Courses', 'text_domain' ),
'not_found' => __( 'No courses found', 'text_domain' ),
'not_found_in_trash' => __( 'No courses found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$rewrite = array(
'slug' => 'post_type',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'Course', 'text_domain' ),
'description' => __( 'Training Course information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-learn-more',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => 'course',
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'courses', $args );
}
add_action( 'init', 'courses_post_type', 0 );
My question is how do I get the the content from the post to display in my custom theme?
I am currently stuck and encountering errors. Here is where I am upto:
I have created a template called single-course.php and for now not changed anything diffrent to my main template.
However when I go to view the post in the browser I get the error message:
Sorry, no posts matched your criteria.
Within single-course.php I have placed the code below inside the <div></div> where I would like the content to display.
If it helps to visual what I am trying to achieve overall is a custom post type (in my case each post is a training course) and a page that displays each individual custom post. Equally it could be shop and custom post is a product where my single-course.php is supposed to display A particular products information inside a div set aside for the purpose.
// This div shows the post title using a wordpress php function
<div class="page-title bg-alpha-moderateblue">
<h1 style="margin:0px;"><?php echo get_the_title(); ?></h1>
</div>
//This div should show the post or in my case the training course overview
//From my understading I have to use the loop although I only want to SHOW THE CONTENT OF A PARTICULAR CUSTOM (course) POST.
<div class="page-content bg-white">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'courses');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
</div>
All constructive feedback and suggestions are greatly appreaciated, thanks in advance