Below, you’ll find a detailed guide on how to add the Related Post Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Related Post Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Related Post Plugin and the shortcodes it provides:
"Related Post is a handy WordPress plugin that effortlessly links related content. It enhances user experience by providing relevant posts and boosts engagement on your site."
- [related_post]
Related Post [related_post] Shortcode
The Related Post shortcode is designed to display related posts on your WordPress site. The shortcode fetches the settings from the ‘related_post_settings’ option and checks if the related post feature is enabled for the current post. If not, it returns false. It retrieves the post type and applies filters to the attributes. The view type is set to ‘grid’ by default, but can be changed via the ‘view_type’ attribute. Finally, it includes the ‘related-post-hook.php’ template file and displays the related posts in a div with the class ‘related-post’.
Shortcode: [related_post]
Parameters
Here is a list of all possible related_post shortcode parameters and attributes:
post_id
– Specifies the unique ID of the post.post_ids
– Represents multiple post IDs, separated by commas.headline
– Defines the headline for the related posts section.view_type
– Sets the display style of the related posts (for example, ‘grid’).
Examples and Usage
Basic example – The following shortcode displays related posts for a specific post by referencing its ID.
[related_post post_id=5 /]
Advanced examples
Displaying related posts for multiple specific posts by referencing their IDs. The plugin will fetch related posts for each ID provided.
[related_post post_ids="5,8,10" /]
Displaying related posts with a custom headline and a specific view type. This shortcode allows you to customize the headline of the related posts section and specify the layout of the posts.
[related_post post_id=5 headline="Related Articles" view_type="list" /]
Displaying related posts without specifying any post ID. This shortcode will fetch related posts for the current post being viewed.
[related_post /]
PHP Function Code
In case you have difficulties debugging what causing issues with [related_post]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('related_post', 'related_post_display');
Shortcode PHP function:
function related_post_display($atts, $content = null)
{
$atts = shortcode_atts(
array(
'post_id' => "",
'post_ids' => "",
'headline' => "",
'view_type' => "",
),
$atts
);
$related_post_settings = get_option('related_post_settings');
$post_id = isset($atts['post_id']) ? (int) $atts['post_id'] : get_the_ID();
$related_post_enable = get_post_meta($post_id, 'related_post_enable', true);
if ($related_post_enable == 'yes') return false;
$post_type = get_post_type($post_id);
$atts['settings'] = $related_post_settings;
$atts['post_type'] = $post_type;
$atts = apply_filters('related_post_atts', $atts);
$view_type = isset($atts['view_type']) ? $atts['view_type'] : 'grid';
$layout_type = !empty($view_type) ? $view_type : $related_post_settings['layout_type'];
require_once(related_post_plugin_dir . 'templates/related-post-hook.php');
ob_start();
?>
<div class="related-post <?php echo $layout_type; ?>">
<?php
do_action('related_post_main', $atts);
?>
</div>
<?php
return ob_get_clean();
}
Code file location:
related-post/related-post/includes/shortcodes.php
Conclusion
Now that you’ve learned how to embed the Related Post Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply