Add Related Post To Genesis Themes Without Plugin

Add Related Post To Genesis Themes Without Plugin

Add Related Post To Genesis Themes Without Plugin

A tutorial on how o add related post to genesis without plugins. Remember that this tutorial is only for Genesis theme framework users. If you are using any other WordPress theme, then this guide is not for you.

In this tutorial you will get simple php codes, which you have to place in custom.php file of your theme. That’s all. The codes provided below will generate the related articles based on categories.

But before we proceed, please follow the precautions listed below :

  • Please create backup of your theme
  • Make sure you copy the exact codes provided
  • Any mistake to custom.php file, can make your blog go down temporally
  • Please use online editor ( provided in cPanel ) to edit php file, or use your ftp program

Add related posts To genesis Theme Based on Categories

/** Display related posts in Genesis based on Category  */

function related_posts_categories() {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$cats = wp_get_post_categories( $post->ID );
$catIDs = array( );{
foreach ( $cats as $cat ) {
$catIDs[] = $cat;
}
$args = array(
'category__in'          => $catIDs,
'post__not_in'          => $postIDs,
'showposts'             => 5,
'ignore_sticky_posts'   => 1,
'orderby'               => 'rand',
'tax_query'             => array(
array(
'taxonomy'  => 'post_format',
'field'     => 'slug',
'terms'     => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote' ),
'operator' => 'NOT IN'
)
)
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
$related .= '<li>' . get_the_title() . '</li>';
}
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_categories' );

Copy the exact php function provide above to your function.php file. Then simply save it.

If you want to change the ” Related Post ” H3 Tag, then just replace it with what you desire. The function provided above will generate list of random posts based on categories.

Show Related Posts Based On Tags

Now below I am proving the function which will generate the random related post links based on tags. I think you will get great diversity using it. It will also show maximum of five links.

/** Display related posts in Genesis based on Tags */

function related_posts_tags () {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$tags = wp_get_post_tags( $post->ID );
foreach ( $tags as $tag ) {
$tagID[] = $tag->term_id;
}
$args = array(
'tag__in'               => $tagID,
'post__not_in'          => $postIDs,
'showposts'             => 5,
'ignore_sticky_posts'   => 1,
'tax_query'             => array(
array(
'taxonomy'  => 'post_format',
'field'     => 'slug',
'terms'     => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote'
),
'operator'  => 'NOT IN'
)
)
);
$tag_query = new WP_Query( $args );
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();
$related .= '<li>' . get_the_title() . '</li>';
$postIDs[] = $post->ID;
$count++;
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_tags' );

If you want to change the number of links generated. Then simply change the ‘showposts’  => 5, to what you desire. The credit for functions goes to wpsquare. I really thank to the author. I am just presenting the article in better manner. I wish this tutorial will help you a lot.

Why You Shouldn’t Use This Method

The methods which I provided in this article is only for those users, who really doesn’t care for server load. Personally I will always suggest you to use related post plugins. Since plugins have the functionality to cache the links. So every time a new visitor comes to your website and when he views any particular page. Then these plugins show the cached links to him.

This method helps you reduce your server load, since functions are not executed every time. This method also improved the total page loading, and improves user experience. Please read this article : Best Related Post Plugins . Also leave your personal feelings and question in comment area.

WEEKLY NEWSLETTER

Grab Weekly Design & Development News, Collections, Tips And Tools.

Privacy Policy : we hate SPAM just like you do

About Vivek Kumar Poddar

Hi Its Vivek, Admin of this blog. I am an engineering student from India who loves web designing ( Genesis ), Photoshop, Wordpress, Google & finally HALO [ series ].Find me on Google+

Comments

  1. I want to download Contact form which Contact form is best please let me know thankxx

Speak Your Mind

*