Forums » Plugin: Related Posts by Taxonomy » How to move related output from content to after post

  • Resolved ABTOP

    (@abtop)


    The official recommendation will not work, don’t even bother.
    If you want to move the related group of links fron the end of post content to the outside of the post content, use a later hook.
    You can put exactly this snippet into you theme’s functions.php:

    //push related post insertion point after post content
    //first we remove the hook from the tail end of content
    global $related;
    remove_filter( 'the_content', array($related, 'related_content_filter') );
    // then we hang related on a later hook
    add_action( 'post_content_after', 'related_content_filter' );
    function related_content_filter(  ) {
    			if ( (get_option( 'related_content', 0 ) == 1 && is_singular()) || get_option( 'related_content_all', 0 ) == 1 ) {
    				global $related;
    				$related_posts = $related->show( get_the_ID() );
    				if ( $related_posts ) {
    					$output = '<div class="related_content" style="clear:both;">';
    					$output .= '<h3 class="widget-title">';
    					$filtered_title = stripslashes(get_option('related_content_title', __('Related Posts', 'related')));
    					$output .= apply_filters( 'related_content_title', $filtered_title );
    					$output .= '</h3>';
    					$output .= $related_posts;
    					$output .= '</div>';
    					echo $output;
    				}
    			}
    			// otherwise returns nothing
    		return ; 
    } 
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi ABTOP

    The the_content hook is what most plugins use because it is available in most themes. The good news is you can use any hook you want if it’s in the correct place in your theme. For you this was post_content_after. This is not a WordPress hook (and therefore not available to most users).

    Home

    I don’t know how you set up the related posts with this plugin, but this plugin doesn’t use global variables to get or display the related posts.

    
    global $related;
    $related_posts = $related->show( get_the_ID() );
    

    Saying the official documentation does not work is very far fetched. I understand you had some trouble displaying the related posts where you wanted, but the code you posted (not in the documentation) will produce errors for users without your custom setup (global object).

    Also, you don’t have to remove a hook if you didn’t add it in the first place.

    
    //first we remove the hook from the tail end of content
    remove_filter( 'the_content', array($related, 'related_content_filter') );
    
    • This reply was modified 6 years, 11 months ago by keesiemeijer.
    • This reply was modified 6 years, 11 months ago by keesiemeijer.
    Thread Starter ABTOP

    (@abtop)

    The official advice doesn’t work, for it doesn’t remove the initial connection into the_content hook. I was getting duplicate insertions — one original, still working perfectly well and the second later displaying as shortcode. I was not able to get rid of the original insertion point until I used
    remove_filter( 'the_content', array($related, 'related_content_filter') );
    to counter your

    // Add the related posts to the content, if set in options
    add_filter( 'the_content', array($this, 'related_content_filter') );

    per another piece of official documentation:

    If a filter has been added from within a class, for example by a plugin, removing it will require accessing the class variable.
    global $my_class;
    remove_filter( 'the_content', array($my_class, 'class_filter_function') );

    You are right about post_content_after. Apparently this was custom for my theme and will not be available just for anybody. I should have double-checked it. Oops about it.

    Generally plugins developers should not make concessions to lazy theme designers. Themes should support the fullest range of hooks, and those which do do not, should not be used.

    At the end of the day, you would do well if you offered alternative hook to insert the Related links.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    This plugin doesn’t insert the links (after the content). That’s why you have to use code in your theme’s functions.php file (or use a shortcode or function).

    This code is not part of the plugin.

    
    // Add the related posts to the content, if set in options
    add_filter( 'the_content', array($this, 'related_content_filter') );

    In what file did you find this?

    If it’s causing duplicate insertions it’s better to remove that line instead of removing the filter.

    Thread Starter ABTOP

    (@abtop)

    related.php line 64

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Did you add it yourself or is this part of your theme? This plugin doesn’t have a file called related.php.
    https://plugins.trac.wordpress.org/browser/related-posts-by-taxonomy/#trunk/includes

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to move related output from content to after post’ is closed to new replies.