Sauter la navigation

[Résolu] Code for displaying table of contents using child & grandchild posts

This support ticket is created Il y a 3 années et 7 mois. 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.

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)

Ce sujet contient 1 réponse, a 2 voix.

Dernière mise à jour par Minesh Il y a 3 années et 7 mois.

Assisté par: Minesh.

Auteur
Publications
#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

Les langues: Anglais (English )

Fuseau horaire: 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.