ThemeShaper Forums » Thematic

Related posts in functions.php

(6 posts)
  • Started 10 years ago by iamww
  • Latest reply from iamww
  • This topic is resolved
  1. Does anyone know how to get "related posts" working in the thematic_postfooter using the functions.php file of a Thematic child theme?

    I have tried various related posts plugins and all of them want to insert the data before the postfooter. I assume this is because the plugin's function is being called in the functions.php rather than in the single.php.

    I've hacked together a solution for now (posted below). This pulls post related by the category. However, it is not as elegant as I would like it to be. Using one of the available "related posts" plugins would allow for more options and related data without having to rewrite it myself.

    // Set the current post's ID equal to a variable to exclude from the query
    $currentpost = $post->ID;
    	// Get post categories and set $cat to first category
    	$cat = get_the_category(); $cat = $cat[0];
    	// Set arguments for the get_posts function more options found at wordpress codex
    	$args = array(
    	     'numberposts' => '10',
    	      'category' => $cat->cat_ID //calls category ID number
            ) ;
    $postrelated .= '<ul>';
    global $post;
    $myposts = get_posts($args); //gets arguments from array above
    foreach($myposts as $post) : //loops through posts
    	if( $post->ID == $currentpost ) continue;
    	setup_postdata($post); //sets up posts
    	$postrelated .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    endforeach;
    $postrelated .= '</ul>';

    Hopefully, this all makes sense and someone else can help. Thanks!

    Posted 10 years ago #
  2. Hey,

    do you have any favorite Related Post Plugins? .. need to look into these to see how these could be integrated according to your needs.

    Cheers,

    Chris

    Posted 10 years ago #
  3. Chris,

    Thanks for your reply.

    I'm pretty flexible as to which plugin to use. However, the Yet Another Related Posts Plugin seems to have the most features and would be the preferred option.

    I just found that Microkid's Related Posts plugin may have an option that can be leveraged to do what I'm looking to.

    According to the plugins information:

    If you’re looking for further integration; this function will return the IDs and titles of related posts:
    <?php $related_posts = MRP_get_related_posts( $post_id ); ?>

    It will return an array like:
    $related_posts[related_post_id]->related_post_title

    I just haven't been able to get it to output from the array.

    Thanks again, Will

    Posted 10 years ago #
  4. Also, just figured out the code I posted earlier breaks the displaying of the comments list. So, that method is shot as well...

    Posted 10 years ago #
  5. Hey Will,

    got it up and running with Yet Another Related Posts Plugin.

    The boolean in the function call does the trick:

    function add_related_posts($postfooter) {
    	if (is_single()) {
    		$postfooter .= related_posts(array('post'),FALSE);
    	}
    	return $postfooter;
    }
    add_filter('thematic_postfooter', 'add_related_posts');

    Without the boolean it'll use ECHO instead of RETURN and you'll end up with having the output before the postheader instead of behind it.

    Cheers,

    Chris

    Posted 10 years ago #
  6. Chris,

    You are THE MAN!

    I ended up using the following code and tacked it onto the other items I have overwritten in the thematic_postfooter.

    if (is_single()) {
    	$postrelated .= related_posts(array('post'),FALSE);
    }

    Thanks for your help on this item and all that you do for Thematic and WordPress users!

    Take care,
    Will

    Posted 10 years ago #

RSS feed for this topic

Reply

You must log in to post.