How to Create a Lightweight Related Posts Shortcode in WordPress
Display Relevant Posts in WordPress Without a Plugin
A Simple WordPress Shortcode to Show Related Posts
Build Your Own show_posts Shortcode in WordPress
Show Relevant WordPress Posts Using WP_Query
WordPress Shortcode for Related Posts with Optional Excerpts
Add Related Posts to WordPress Posts Without Extra Plugins
Lightweight Related Posts in WordPress Using Built-in Search
How to Use WordPress Search to Display Relevant Posts
Custom WordPress Shortcode: Show Matching Posts by Keyword
Wordpress Short Code Function: Showing Relevant PostsA lightweight WordPress shortcode for displaying relevant posts using the built-in search query. Supports custom keywords, post count limits, and optional inline excerpts without needing an extra plugin.
In WordPress, you can create custom shortcode functions by adding code to your theme’s functions.php file, preferably in a child theme.
The following example adds a shortcode called show_posts, which can be used inside your posts to display a list of relevant posts. The relevant posts are retrieved using WordPress’s built-in search mechanism, similar to the /?s=keyword query parameter. You can also specify the number of posts to show and choose whether to display each post’s excerpt inline.
Compared with installing another WordPress plugin, a PHP shortcode like this is lightweight and gives you full control over the code. It can also reduce unnecessary overhead, as long as the implementation is kept simple and secure.
Also, with shortcode, you can choose where to place/show the relevant posts where the plugins only allow you to place them after the posts.
To use it, copy and paste the code below into your functions.php file. Then, inside any post or page, you can use the show_posts shortcode to insert the relevant post list.
For example:
[ show_posts keyword="wordpress" number="8" show_excerpt="true" ]
Will produce:
- How to Use WordPress Search to Display Relevant Posts?
How to Create a Lightweight Related Posts Shortcode in WordPress Display Relevant Posts in WordPress Without a Plugin A Simple… - WordPress: How to Output Full Text in the Feed?
How to Make WordPress RSS /feed Output Full Text WordPress RSS Full Content Output Guide How to Show Full Text… - PHP7.x is deprecated by WordPress: Why You Should Upgrade to PHP8.x?
Time to Say Goodbye to PHP 7.4 — My Blogs Are Finally Getting an Upgrade After Years on PHP 7.4,… - Adding Two Short Code Functions to WordPress: Top Posts By Number of Comments and Top Posts by Ratings
The wordpress shortcode is a great feature that we can easily add via the add_shortcode function. This post introduces two… - Adding a Short Code Function to Include Any PHP or HTML Files in the WordPress Posts or Pages
Sometimes, we want to include the PHP or HTML or external files in a wordpress post or page, then we… - Free WordPress T-Shirt by Submitting a Bug Report
WordPress is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB… - 5 Things to Look For in a WordPress Hosting Provider
Web hosting is the backbone of the website world. Without it, no website can exist on the Internet. Web hosting… - Password Protect or IP Restriction on WordPress wp-admin Folder (htaccess and htpasswd)
wp-admin folder is the most important folder in a wordpress installation. It mainly contains the code for the Dashboard. However,…
[ show_posts keyword="php" number="5" show_excerpt="false" ]
Will produce:
- PHP7.x is deprecated by WordPress: Why You Should Upgrade to PHP8.x?
- CloudFlare: Change Security Level Value Programmatically for Multiple Domains via PHP/Python/Bash Script
- Adding a Short Code Function to Include Any PHP or HTML Files in the WordPress Posts or Pages
- PHP Code to Redirect with 302 and a Referer
- How to Enable and Monitor the Slow Logs for PHP-FPM?
- Handling Command Line Parameters for PHP Script (Turn Parameters into GET/POST)
- Convert UTF Encoding to UCS in PHP
- A Simple PHP Function to Disable Google Fonts in WordPress
By default, number is set to 8 posts, and the posts show_excerpt is set to hide.
function show___posts_func($atts) {
$atts = shortcode_atts(
array(
'keyword' => '',
'number' => 5,
'show_excerpt' => 'true',
),
$atts,
'show_posts'
);
$keyword = sanitize_text_field($atts['keyword']);
$number = max(1, min(20, intval($atts['number'])));
$show_excerpt = filter_var($atts['show_excerpt'], FILTER_VALIDATE_BOOLEAN);
if ($keyword === '') {
return '';
}
$query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
's' => $keyword,
'posts_per_page' => $number,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
));
if (!$query->have_posts()) {
return '';
}
$output = '<ul class="show-posts-list">';
while ($query->have_posts()) {
$query->the_post();
$output .= '<li>';
$output .= '<a href="' . esc_url(get_permalink()) . '">';
$output .= esc_html(get_the_title());
$output .= '</a>';
if ($show_excerpt) {
$excerpt = get_the_excerpt();
if ($excerpt) {
$output .= '<br><small>' . esc_html(wp_trim_words($excerpt, 20)) . '</small>';
}
}
$output .= '</li>';
}
$output .= '</ul>';
wp_reset_postdata();
return $output;
}
add_shortcode('show_posts', 'show___posts_func');
Wordpress is King!
- Wordpress Short Code Function Showing Relevant Posts
- Wordpress: How to Output Full Text in the Feed?
- How to Add Adsense Ads to bbPress Forum?
- Adding Two Short Code Functions to Wordpress: Top Posts By Number of Comments and Top Posts by Ratings
- Adding a Short Code Function to Include Any PHP or HTML Files in the WordPress Posts or Pages
- Free Wordpress T-Shirt by Submitting a Bug Report
- 5 Things to Look For in a WordPress Hosting Provider
- Using the Regular Expression to Replace External Links in Wordpress for SEO purposes
- How to Add a ShortCode to Include a PHP File in Wordpress?
–EOF (The Ultimate Computing & Technology Blog) —
707 wordsLast Post: Microsoft Edge Settings That May Be Slowing Down Your Windows PC
