How to Add Related Posts in WordPress Without a Plugin
Published: 2024-07-01 16:55:24

Adding related posts to your WordPress site can significantly enhance user engagement by encouraging readers to explore more content. While there are numerous plugins available for this purpose, you might prefer to achieve this without adding extra plugins to keep your site lightweight and fast. Here’s a step-by-step guide to adding related posts in WordPress without using a plugin.

## Step 1: Create a Child Theme

Before making any changes to your theme's files, it's essential to create a child theme. This ensures that your modifications won't be lost when the theme is updated.

1. **Create a child theme folder:** In your WordPress installation directory, navigate to `wp-content/themes` and create a new folder for your child theme.
2. **Create a stylesheet:** Inside your child theme folder, create a `style.css` file with the following content:
    ```css
   /*
    Theme Name: Your Child Theme Name
    Template: your-parent-theme
    */
    ```
3. **Create a functions file:** In the same folder, create a `functions.php` file. You can leave it empty for now.

#### Step 2: Add the Related Posts Functionality

To display related posts based on categories or tags, you need to add custom code to your child theme's `functions.php` file. Here’s an example of how to display related posts based on categories:

1. **Edit `functions.php`:** Open the `functions.php` file in your child theme and add the following code:
    ```php
    function wpb_related_posts_by_category() {
        global $post;

        // Get the current post ID
        $post_id = $post->ID;

        // Get the categories of the current post
        $categories = get_the_category($post_id);

        if ($categories) {
            $category_ids = array();

            foreach ($categories as $category) {
                $category_ids[] = $category->term_id;
            }

            // Query for related posts
            $query_args = array(
                'category__in'   => $category_ids,
                'post__not_in'   => array($post_id),
                'posts_per_page' => 5, // Number of related posts to display
                'ignore_sticky_posts' => 1
            );

            $related_posts = new WP_Query($query_args);

            if ($related_posts->have_posts()) {
                echo '<div class="related-posts">';
                echo '<h3>Related Posts</h3>';
                echo '<ul>';

                while ($related_posts->have_posts()) {
                    $related_posts->the_post();
                    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
                }

                echo '</ul>';
                echo '</div>';
            }

            // Reset Post Data
            wp_reset_postdata();
        }
    }
    ```

2. **Call the Function in Your Theme Files:** You need to call this function in your theme's single post template file (`single.php`) to display the related posts. Open `single.php` and add the following line where you want the related posts to appear:
    ```php
    <?php wpb_related_posts_by_category(); ?>
    ```

#### Step 3: Style the Related Posts Section

To make the related posts section visually appealing, you can add some custom CSS. Open your child theme’s `style.css` file and add the following styles:
```css
.related-posts {
    margin-top: 30px;
    padding: 15px;
    border-top: 1px solid #ddd;
}

.related-posts h3 {
    margin-bottom: 15px;
    font-size: 1.5em;
}

.related-posts ul {
    list-style: none;
    padding: 0;
}

.related-posts ul li {
    margin-bottom: 10px;
}

.related-posts ul li a {
    text-decoration: none;
    color: #0073aa;
}

.related-posts ul li a:hover {
    text-decoration: underline;
}
```

#### Conclusion

By following these steps, you can add related posts to your WordPress site without using a plugin. This approach allows you to customize the functionality and appearance to match your theme perfectly. Remember to always back up your site before making any changes to your theme files.

Adding related posts can significantly improve user engagement and keep visitors on your site longer, ultimately enhancing the overall user experience.