ernilambar/wp-related-posts

WordPress related posts

2.0.0 2022-10-11 14:52 UTC

This package is auto-updated.

Last update: 2024-04-30 00:38:23 UTC


README

WordPress related posts helper class to fetch related posts based on post's taxonomies.

Requirements

  • WordPress 5.1+.
  • PHP 5.6+.
  • Composer for managing PHP dependencies.

Installation

First, you'll need to open your command line tool and change directories to your theme folder.

cd path/to/wp-content/themes/<your-theme-name>

Then, use Composer to install the package.

composer require ernilambar/wp-related-posts

Assuming you're not already including the Composer autoload file for your theme and are shipping this as part of your theme package, you'll want something like the following bit of code in your theme's functions.php to autoload this package (and any others).

The Composer autoload file will automatically load up the package for you and make its code available for you to use.

if ( file_exists( get_parent_theme_file_path( 'vendor/autoload.php' ) ) ) {
  require_once get_parent_theme_file_path( 'vendor/autoload.php' );
}

Usage

You can fetch related post using posts method.

use Nilambar\WPRelatedPosts\RelatedPosts;

$args = array(
  'number'     => 3,
  'taxonomies' => array( 'post_tag' ),
);

$related_posts = RelatedPosts::posts( get_the_ID(), $args );

Copyright and License

This project is licensed under the MIT.

2022 © Nilambar Sharma.