# Previous and Next Lesson Links Within a Category

> Adds "Previous lesson" and "Next lesson" links at the end of posts in your course categories, in publishing order, so readers can work through a series.

- Type: PHP
- Runs: Frontend only
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: lessons, navigation, series
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/lesson-series-navigation/

## Code

```php
// Category slugs that hold lesson series.
$wpide_course_categories = array( 'course-1' );

add_filter( 'the_content', function ( $content ) use ( $wpide_course_categories ) {
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() || ! has_category( $wpide_course_categories ) ) {
        return $content;
    }

    $prev = get_adjacent_post( true, '', true, 'category' );
    $next = get_adjacent_post( true, '', false, 'category' );

    if ( ! $prev && ! $next ) {
        return $content;
    }

    $nav = '<nav class="wpide-lesson-nav" aria-label="Lesson navigation" style="display:flex;flex-wrap:wrap;justify-content:space-between;gap:1em;margin-top:2em">';
    $nav .= $prev ? '<a rel="prev" href="' . esc_url( get_permalink( $prev ) ) . '">&larr; ' . esc_html( get_the_title( $prev ) ) . '</a>' : '<span></span>';
    $nav .= $next ? '<a rel="next" href="' . esc_url( get_permalink( $next ) ) . '">' . esc_html( get_the_title( $next ) ) . ' &rarr;</a>' : '';

    return $content . $nav . '</nav>';
} );
```

## Notes

List the category slugs that are courses in $wpide_course_categories. Links stay inside the same category.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Previous and Next Lesson Links Within a Category" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
