• niccoicco9

    (@niccoicco9)


    Hello, i need to get the current lesson id in the single lesson template because i need to use advanced custom field in the template and i need the id of the post but not of the coure, of the lesson. by default if i get post_id i get the id of the course but i need lesson post id.

    content-item.php

    <?php
    /**
     * Template for displaying item content in single course.
     *
     * This template can be overridden by copying it to yourtheme/learnpress/single-course/content-item.php.
     *
     * @author   ThimPress
     * @package  Learnpress/Templates
     * @version  3.1.0
     */
    
    /**
     * Prevent loading this file directly
     */
    defined( 'ABSPATH' ) || exit();
    global $post;
    $user           = LP_Global::user();
    $course_item    = LP_Global::course_item();
    $course         = LP_Global::course();
    $can_view_item  = $user->can_view_item( $course_item->get_id(), $course->get_id() );
    $block_by_check = $course_item->is_blocked_by( $user->get_id(), $course->get_id() );
    $course_id = get_the_id();
    $courseProva = learn_press_get_course( $course_id );
    $curriculum = $courseProva->get_curriculum();
    
    foreach ( $curriculum as $section ) {
        // Get the lessons associated with the section.
        $lessons = $section->get_items();
    
        // Iterate over each lesson.
        foreach ( $lessons as $lesson ) {
            // Now you can work with each lesson, for example:
    		
    			
            // Get the lesson ID.
            $lesson_id = $lesson->get_id();
    		echo $lesson_id;
    		echo '******';
        }
    	
    }
    
    ?>

    with this method i can get all lesson id of the course but i need the current.
    anyone can help me?

    • This topic was modified 5 years ago by niccoicco9.
    • This topic was modified 5 years ago by niccoicco9.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Computing4All

    (@computingforall)

    I am facing the same problem. I am trying to get the post ID of the current lesson. get_the_ID() from a lesson is returning the ID of the course. Some plug-ins rely on the ID of the loaded post. Those plugins are not working for lessons.

    benediktg

    (@benediktglass)

    <?php

    // Get the course instance.
    $course = learn_press_get_course( $course_id );

    // Get the curriculum of the course.
    $curriculum = $course->get_curriculum();

    // Iterate over the curriculum sections.
    foreach ( $curriculum as $section ) {
    // Get the lessons associated with the section.
    $lessons = $section->get_items();

    // Iterate over each lesson.
    foreach ( $lessons as $lesson ) {
    // Now you can work with each lesson, for example:

    // Get the lesson ID.
    $lesson_id = $lesson->get_id();
    }
    }

    ?>

    Props to him: https://stackoverflow.com/a/60958273/14099802

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Get lesson id in the single lesson template’ is closed to new replies.