0
  • I have a custom post type 'courses', with a course description as post_content, and number of attachments
  • I also have a custom post type 'course units', linked to the courses with a custom field 'course-id'

  • I created a template file for the course dashboard, showing a list of the units, and a couple of links - to the course description, and to an archive of the attachments.

My (pretty generic) question:

  • I'm stuck at how to create/implement the seperate 'pages' for the description, and the attachment archive - can't get my head around it.

Some additional info:

Any ideas / suggestions?

2 Answers 2

1

You may use template redirect action to check if there is parameter set for description and based on that you can show desired template.

<?php 

add_action('template_redirect', 'course_template_redirect', 1);

function course_template_redirect()
{
    global $wp_query;
    if($wp_query->post_type=='courses' and $_REQUEST['showdesc']=='1')
    {
        //include your template from your theme folder. for e.g include('mytheme/coursetemplate.php');

    }
}

?>
3
  • that seems like a perfect solution, thanks for pointing me to 'template_redirect', never heard of it. just one issue: for some reason, the original course template file (single-course.php) is appended after the closing body tag - I suppose I also have to 'exclude' this template file in some way - any idea how to to this?
    – ptriek
    Commented Apr 23, 2013 at 13:06
  • Also - just for future reference - I had to change $wp_query->post_type to get_post_type() to make it work...
    – ptriek
    Commented Apr 23, 2013 at 13:07
  • Already found the fix: just had to add exit() after the include, this prevents the original template from being loaded.
    – ptriek
    Commented Apr 23, 2013 at 13:12
1

If the description and attachments require separate URLs then I would create the 'attachments' as a separate post type to the 'courses' which contain the description.

Then from your main custom post type 'courses' I would create reciprocal relationships from each course to its units and attachments using the Posts 2 Posts plugin.

1
  • Could be a good workaround indeed, thanks for the suggestion. I prefer the solution by Rajeev, however, it's a bit more elegant - no need for extra post types...
    – ptriek
    Commented Apr 23, 2013 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.