[Gelöst] Code for displaying table of contents using child & grandchild posts
This support ticket is created vor 2 Jahre, 10 Monate. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
In the earlier ticket, the support agent had helped me set up Views for displaying table of contents.
However, I am finding it a time-consuming process to replicate it on another site and instead, thinking of writing a small plugin or function that would display the table of contents.
Course <== Main Post
----- Chapter <== Child Post
---------- Lesson <= Grandchild Post
So far, this is what I have managed but unsure about the best coding practises:
<?php
function get_course_id(){
global $post;
$type = isset($post->post_type) ? $post->post_type : null;
$id = 0;
if ( !in_array( $type, array( 'course', 'chapter', 'lesson' ) ) ) return $id;
switch ($type) {
case 'course':
$id = $post->ID;
break;
case 'chapter':
$id = toolset_get_related_post( $post->ID, 'course-chapter');
break;
case 'lesson':
$chapter_id = toolset_get_related_post( $post->ID, 'chapter-lesson');
$id = toolset_get_related_post( $chapter_id, 'course-chapter');
break;
}
}