• I wanted to display the related posts/pages in-line with the post. (So that the related posts display after the first paragraph).

    I have managed to hack something together for my purposes, but it would be really nice if this feature could be added as an option in version 2.

    You can seen an example of the rendering at:
    https://www.momentummt.ca/MassageTherapy/2007/10/01/plantar-fasciitis-a-real-pain-in-the-foot/

    To do this:
    1) I added a new filter in yarpp.php
    add_filter('the_content', 'yarpp_mmt_related');

    2) I commented out the following line in yarpp.php
    add_filter('the_content','yarpp_default');

    3) In magic.php – I changed line 30 as follows:
    $string = str_repeat($post->post_title, $w['title'].' ').str_repeat(str_replace('-', ' ', $post->post_name).' ', $w['name']).str_repeat(strip_tags(apply_filters_without('the_content',$post->post_content,'yarpp_mmt_related')), $w['content'].' ');//mitcho: strip_tags

    4) Finally I wrote the following function. Obviously with the hardcoding currently in place it is specific to my needs.

    function yarpp_mmt_related($content) {
    	global $wpdb, $post, $user_level;
    	if (is_single()) {
    		// get related posts
    		$related_mmt = yarpp_related(array('post'),array(),false);
    		// find the the first close paragraph tag
    		$pos = strpos($content, '</p>');
    		// insert the related entries into the article
    		$content = substr_replace($content, '</p><div id=\'relposts\'><h3>Related Articles</h3>'.$related_mmt.'</div>', $pos, 4);
    		// send it back
    		return $content;
    	} else {
    		return $content;
    	}
    }

    Thanks for considering this,
    Bruce.

Viewing 3 replies - 1 through 3 (of 3 total)
  • This is excellent and very similar to what I am looking for.

    The only real difference would be the ability to place the related posts within a post using a shortcode eg. [rel_post] or %rel_post% or something similar, as nearly all of my posts include an image and having the related posts appear after the first paragraph may mess up the layout somewhat.

    How does your coding above gel with the current release 2.1.6, of YARPP?

    It would be nice if the ability to show the related posts in-line could be included soon!

    Hi Bruce–I somehow missed this post, but it’s a neat idea. I have no current plans to put this into YARPP itself, though.

    Yes, the code Bruce gave should work with 2.1.6, but there is an easier way.

    1. No need to comment out the add_filter('the_content','yarpp_default'); line… simply make sure to turn off the “automatically display” option.
    2. YARPP 2.1.5 added an echo parameter to the related_*() functions, so you can simply get related_posts(array(),false) and it will return the related posts HTML as a string to you.
    3. Use this instead of yarpp_related(array('post'),array(),false).

    This, of course, is if you want to follow Bruce’s plan to simply have the related posts show up right after the first paragraph. If you want to show the related posts wherever %rel_post% is, you can write a filter to replace that text with the related posts display, as seen above. If you have trouble with this (creating infinite loops, etc.) try putting the filter name into the YARPP blacklist array (just search in includes.php).

    I hope this helps!

    Hi Mitch – thanks for the quick response and for your suggestions.

    Not being a coder, it is a little above my head, but I will play around with your suggestions and see how I go.

    Many thanks for the excellent plugin and the help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Yet Another Related Posts Plugin] YARPP Feature Suggestion: Display Related Posts in-line’ is closed to new replies.