logo

$20
How can I add a list of related posts by tag within my RSS feed?

In the RSS for my site, at the bottom of each entry I want an unordered list of related posts by tag. I tried this but it didn't work -



<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);

?>

Jen Tidwell | 12/07/09 at 10:37pm | Edit


(8) Possible Answers Submitted...

  • avatar
    Last edited:
    12/08/09
    12:54pm
    Jean-Baptiste Jung says:

    Partial solution, hope it helps you or another developer :

    <?php
    function insertRelatedPosts($content) {
    // Code to get the related posts goes here
    // see http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin
    }
    add_filter('the_excerpt_rss', 'insertRelatedPosts');
    add_filter('the_content_rss', 'insertRelatedPosts');
    ?>

  • avatar
    Last edited:
    12/08/09
    12:55pm
    Dan Davies says:


    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

    $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'showposts'=>5, // Number of related posts that will be shown.
    'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
    echo '<h3>Related Posts</h3><ul>';
    while ($my_query->have_posts()) {
    $my_query->the_post();
    ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php
    }
    echo '</ul>';
    }
    }
    ?>

  • avatar
    Last edited:
    12/08/09
    1:29pm
    nvartolomei says:

    <?php
    /*
    * use this within the loop
    */

    /*
    * initialize $tags variable, although php will output notice
    */
    $tags = NULL;

    /*
    * we need to get all tags of current posts separated by comma
    */
    foreach(get_the_tags() as $tag) {
    $tags .= $tag->name . ",";
    }

    /*
    * we already have all tags listed in $tags variable,
    * now we need to get 5 posts with that tags,
    * I didn't set the limit, becouse by deafult it's 5
    */
    $posts_to_operate_with = get_posts('tag=' . $tags);

    /*
    * we need to know how many posts we get
    */
    $number_of_posts = count($posts_to_operate_with);

    /*
    * if we have posts then go next
    */
    if ($number_of_posts > 1) {

    if ($number_of_posts < 5) {

    /*
    * if number of posts is not equal to 5 then it will be the limit
    */
    $limit = $number_of_posts;
    }

    /*
    * open unordered list tag
    */
    echo "<ul>";
    for($i = 0; $i < $limit; $i++) :
    $my_post = $posts_to_operate_with[$i];

    /*
    * display link for each post
    */
    ?>

    <li><a href="<?php echo $my_post->guid; ?>"><?php echo $my_post->post_title; ?></a></li>

    <?php endfor;

    /*
    * close unordered list tag
    */
    echo "</ul>";
    }
    ?>

  • avatar
    Last edited:
    12/08/09
    1:29pm
    nvartolomei says:

    And yeah, I'm not a wp developer.

  • avatar
    Last edited:
    12/08/09
    1:31pm
    Arun Balasubramanian says:

    Other than the above codes, Here is a simple plugin to check it out (if you want) "Related Posts by Tags" plugin, which simply can do any/all of the following..
    1. Related posts by tags in a widget
    2. Related posts by tags under a single post and
    3. Related posts by tags under every post in Rss Feed

  • avatar
    Last edited:
    12/09/09
    2:51am
    whiking says:

    Im using the plugin (yarpp) in order to display related posts in my posts. However there is also an option to include this into your rssfeed, thru a simple checkbox.

    There are some options that you can tweak to your liking, like on what to put xtra weight on, categories and/or tags, or perhaps date and so forth and so on.

    Read more about the plug in over @ wordpress/extends

    Good Luck
    //w

  • avatar
    Last edited:
    12/10/09
    6:03pm
    Gilbert Pellegrom says:

    Hi. Here is a working and tested implementation. Simply put this code in your "functions.php" file.


    function feed_related_posts($content)
    {
    global $post;
    $showposts = 5;

    if(is_feed()){
    $tags = wp_get_post_tags($post->ID);
    if($tags) {
    $first_tag = $tags[0]->term_id;
    $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>$showposts,
    'caller_get_posts'=>1
    );

    $my_query = new WP_Query($args);
    if($my_query->have_posts()) {
    $content .= '<h3>Related Posts</h3>';
    $content .= '<ul>';
    while ($my_query->have_posts()) {
    $my_query->the_post();
    $content .= '<li><a href="'. get_permalink() .'" title="Permanent Link to '. the_title_attribute('echo=0') .'">'. get_the_title() .'</a></li>';
    }
    $content .= '</ul>';
    }
    }
    }
    return $content;
    }
    add_filter('the_content','feed_related_posts');

  • avatar
    Last edited:
    12/09/09
    12:13pm
    Adam Kayce says:

    Use the "Yet Another Related Posts Plugin" : http://mitcho.com/code/yarpp/ and http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/ .

    Quick and easy.

This question has expired.





Current status of this question: Completed