如何在Learndash的课程中获得第一个主题

时间:2018-11-15 03:41:24

标签: wordpress plugins

我想在learningdash的课程中检索第一个主题,并通过它进行重定向。但是由于某种原因,我不确定该怎么做。

我检查了API,但没有看到任何合适的过滤器/钩子。

这是我的代码:

  function redirect_to_first_topic() {
// We only want to do this for Topics. But the below code can be adapted to work for Lessons
global $post;
$post_id = $post->ID;

if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
    $progress = learndash_get_course_progress( null, $post_id );
    $link = $progress['next'];

    $parent_lesson_id = learndash_get_setting( $post, 'topic' );
    $parent_lesson = get_post( $parent_lesson_id );
    var_dump($parent_lesson);
}

// Always return $link
// return $link;

}

add_action('wp', 'redirect_to_first_topic');

因此,基本上,这里的工作是上课。

2 个答案:

答案 0 :(得分:0)

有点晚了,但是它可以帮助别人。您可以查看如何上一门课程,并在需要时深入学习。第一节课的第一个主题,等等:

add_action( 'template_redirect', 'course_overview_redirect' );

function course_overview_redirect() {
global $post;

if ( ! empty( $post ) && $post->post_type == 'sfwd-courses' && ! empty( $post->ID ) ) {

    $lessons = learndash_get_course_lessons_list( $post->ID );

    if ( ! empty( $lessons ) ) {
        $lesson = array_shift( $lessons );

        if ( ! empty( $lesson ) ) {
            $url = get_permalink( $lesson[ "post" ]->ID );

            if ( ! empty( $url ) ) {   
                wp_redirect( esc_url( $url ) );  // And redirect if needed
                exit;
            }
        }
    }
}
}

答案 1 :(得分:0)

你可以添加这样的按钮;

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'sfwd-lessons', // you can change here to find topics : 'sfwd-topics'
    'suppress_filters' => true,
    'fields' => 'ids',
    'orderby' => 'menu_order', // ordering by menu_order will show lesson list, in their order in course. 
    'order' => 'ASC',
    // this meta query is used if you want to search a lesson under a course, or if you search for a topic which is in a course but not under a lesson                      
    'meta_query' => array(      
                 array(
                    'key'     => 'course_id',
                    'value'   => $course_id, // this is id of your course
                 )
    // if your topic is under a lesson than you should add lesson meta query
    // your meta query should be changed like below
    /* 
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'key'   => 'course_id',
            'value' => $course_id,
        ),
        array(
            'key'   => 'lesson_id',
            'value' => $lesson_id,
        ),
    ),
    */
            ),                  
    
);

$all_lessons = get_posts($args);    

$first_lesson = $all_lessons[0]; // taking directly first element of array  

$button_html = '<a href="' . esc_url( get_post_permalink( $first_lesson ) ) . '" class="button">' . __( 'Start the course', 'custom-text' ) . '</a>';