Below, you’ll find a detailed guide on how to add the Contextual Related Posts 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 Contextual Related Posts Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Contextual Related Posts Plugin and the shortcodes it provides:
"Contextual Related Posts is a powerful WordPress plugin designed to enhance user engagement by displaying related content. It helps to increase your site's dwell time, improving SEO."
- [crp]
Contextual Related Posts [crp] Shortcode
The Contextual Related Posts (CRP) shortcode is a powerful tool for displaying related posts on your website. Using the ‘crp’ shortcode, you can customize the related posts output based on various parameters such as category, post IDs, and meta keys. This shortcode also allows you to control the heading display and offset of related posts.
Shortcode: [crp]
Parameters
Here is a list of all possible crp shortcode parameters and attributes:
heading
– Determines if a heading is displayed (1) or not (0).is_shortcode
– When set to 1, it tells the function that it’s being used as a shortcode.offset
– It sets the number of initial posts to skip in the output.include_cat_ids
– Includes posts belonging to specific category IDs.include_post_ids
– Includes specific post IDs in the output.related_meta_keys
– Specifies meta keys to fetch related posts.
Examples and Usage
Basic example – Utilizes the shortcode to display related posts with default settings.
[crp /]
Advanced examples
Displays related posts with a custom heading and an offset of 2, meaning it will skip the first two related posts.
[crp heading="Recommended Posts" offset=2 /]
Displays related posts from specific categories and posts. Here, you replace ‘cat1, cat2’ with your category IDs and ‘post1, post2’ with your post IDs.
[crp include_cat_ids="cat1, cat2" include_post_ids="post1, post2" /]
Displays related posts based on specific meta keys. Replace ‘key1, key2’ with your meta keys.
[crp related_meta_keys="key1, key2" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [crp]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'crp', 'crp_shortcode' );
Shortcode PHP function:
function crp_shortcode( $atts, $content = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
global $crp_settings;
$crp_settings = crp_get_settings();
$atts = shortcode_atts(
array_merge(
$crp_settings,
array(
'heading' => 1,
'is_shortcode' => 1,
'offset' => 0,
'include_cat_ids' => '',
'include_post_ids' => '',
'related_meta_keys' => '',
)
),
$atts,
'crp'
);
return get_crp( $atts );
}
Code file location:
contextual-related-posts/contextual-related-posts/includes/modules/shortcode.php
Conclusion
Now that you’ve learned how to embed the Contextual Related Posts 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