Skip Navigation

[Resolved] Code for displaying table of contents using child & grandchild posts

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 1 year, 8 months ago.

Assigned support staff: Minesh.

Author
Posts
#2073169

Hello,

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;
}
}

// Display related child posts of Course
$course_chaper_child_posts = toolset_get_related_posts( get_course_id() );
foreach ($course_chapter_child_posts as $course_chapter_child_post) { ?>
<h5 class="<?php echo active-inactive-class(); ?>"><?php echo $chouse_chapter_child_post->post_title; ?></h5>
<?php $chaper_lesson_child_posts = toolset_get_related_posts( get_course_id() );
// Display related child posts of Chapter
$chapter_lesson_child_posts = toolset_get_related_posts( get_course_id() );
foreach ($chapter_lesson_child_posts as $chapter_lesson_child_post) { ?>
<h5 class="<?php echo active-inactive-class(); ?>"><?php echo $chapter_lesson_child_post->post_title; ?></h5>
<?php } ?>
<?php }

?>

Can you please suggest any necessary corrections in the above code?

If you wish, I can provide temporary access to my WP site with all the posts already created.

Thanks,

Alok

#2073645

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The thing is that - Toolset post relationship API function toolset_get_related_posts() needs multiple arguments as you can see with the following doc:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Please check the different examples given with the above doc and try to adjust your code.