Tuesday, August 14, 2012

Thesis Themes Customization: 8 | Add related post after Post or Page

I will use 1.8 Developer version of Thesis Theme

LogIn to your Dashboard and Go to Thesis > Custom File Editor
Thesis Themes Customization, Thesis Themes add category Nav menu after Header, DIY thesis theme place category Nav menu after Header, add category Nav menu after Header in thesis theme 
Select custom_functions.php form drop down menu and Click Edit Selected File.
Thesis Themes Customization, Thesis Themes add category Nav menu after Header, DIY thesis theme place category Nav menu after Header, add category Nav menu after Header in thesis theme

Place this code below custom_functions.php :
//related post shortcode
function related_posts_shortcode( $atts ) {
    extract(shortcode_atts(array(
        'limit' => '7',
    ), $atts));
    global $wpdb, $post, $table_prefix;
    if ($post->ID) {
        $retval = '
<ul>';
        // Get tags
        $tags = wp_get_post_tags($post->ID);
        $tagsarray = array();
        foreach ($tags as $tag) {
            $tagsarray[] = $tag->term_id;
        }
        $tagslist = implode(',', $tagsarray);
        // Do the query
        $q = "
            SELECT p.*, count(tr.object_id) as count
            FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
            WHERE tt.taxonomy ='post_tag'
                AND tt.term_taxonomy_id = tr.term_taxonomy_id
                AND tr.object_id  = p.ID
            AND tt.term_id IN ($tagslist)
                AND p.ID != $post->ID
                AND p.post_status = 'publish'
                AND p.post_date_gmt < NOW()
            GROUP BY tr.object_id
            ORDER BY count DESC, p.post_date_gmt DESC
            LIMIT $limit;";
        $related = $wpdb->get_results($q);
        if ( $related ) {
            foreach($related as $r) {
            $retval .= '
    <li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
        }
        } else {
            $retval .= '
    <li>No related post found :(</li>
';
        }
        $retval .= '</ul>
';
        return $retval;
    }
    return;
}
add_shortcode('related_posts', 'related_posts_shortcode');
function single_post_relatedpost() {
if (is_single()) { ?>
<h3>Related Post:</h3>
<?php echo do_shortcode("[related_posts]"); ?>
<?php }
}
add_action('thesis_hook_after_post', 'single_post_relatedpost');
 Now save it.
[Please comment for any Problem]

No comments:

Post a Comment

!! COMMENT HERE FOR ANY PROBLEM !!

Featured post

WordPress yoast plugin remove date or article:published_time

Yoast is an essential plugin for WordPress SEO. It is one of the best SEO plugin for wordpress right now. But, after disabling date from ...