How to add “Related posts” in content of WordPress blog?
To show related articles you can use the plugin Better Related Posts, Yet Another Related Posts Plugin… However, with a plugin you will not display the related articles in content professional as the photo below? This article will guide you How to add “Related posts” in content of your WordPress blog:
Step 1: Install and active the plugin Related Posts by Taxonomy
Step 2: Add the following code to the functions.php of your blog
// Code for count the number of paragraph
function
count_paragraph(
$insertion
,
$paragraph_id
,
$content
) {
$closing_p
=
'</p>'
;
$paragraphs
=
explode
(
$closing_p
,
$content
);
foreach
(
$paragraphs
as
$index
=>
$paragraph
) {
if
( trim(
$paragraph
) ) {
$paragraphs
[
$index
] .=
$closing_p
;
}
if
(
$paragraph_id
==
$index
+ 1 ) {
$paragraphs
[
$index
] .=
$insertion
;
}
}
return
implode(
''
,
$paragraphs
);
}
// Code for insert related post in content
add_filter(
'the_content'
,
'prefix_insert_post_ads'
);
function
prefix_insert_post_ads(
$content
) {
$related_posts
= do_shortcode(
'[related_posts_by_tax title=""]'
);
if
( is_single() ) {
return
count_paragraph(
$related_posts
, 1,
$content
);
}
return
$content
;
}
Enjoy!