0
votes

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

3
I may be off, but have you tried going to Settings>Permalinks, and just clicking the "Save" button? Sometimes that fixes redirection issues. - coopersita
Also, if the template is named properly, you shouldn't need a custom loop. The default loop should know what to do. - coopersita

3 Answers

0
votes

I'm not sure if you wrote all that code (from your functions.php file) yourself or if you snagged it from somewhere else but I recommend using the example code at https://codex.wordpress.org/Post_Types#Custom_Post_Types with as little customization as possible. You may also want to use the code from Wordpress' TwentySixteen single template (https://github.com/WordPress/twentysixteen/blob/master/single.php) after all, who knows Wordpress better than Wordpress? I don't mean use the code in production per say just as a starting point to help you eliminate the possibility that the error is in those two blocks of code.

That said, have you tried renaming single-course.php to single-courses.php? After all that's the name you specified in the register_post_type call.

Hope that helps, good luck!

0
votes

Problem solved when using the code generator I had failed to change the default 'slug'=> 'post_type' to my custom post type name, in my case "courses".

0
votes

On a side note if you are using single-courses.php you shouldn't need to use

<?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>

If the theme template is named correctly you should just be able to use the standard wordpress loop and markup to display your content.

ie. no need to call another query.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

//Content

<?php endwhile; endif; ?>

Unless of course you are modifying the query to get related content within the single.

In which case I'd suggest using get_posts(); with a simple foreach.

$args = array(
  'post_type' = 'courses',
  'posts_per_page' = -1,
);

<?php $related_posts = get_posts($args);

if ( $related_posts ) : ?>

<?php $temp_post = $post; //temporarily store the $post query for use after the related content
foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>

  <h3><?php the_title(); ?></h3>

  <?php the_content(); ?>

<?php endforeach;
$post = $temp_post; //set the post back to the main query 
endif; ?>

Wordpress codex for get_posts