WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help with Filter hook (3 posts)

  1. Tomas Mackevicius
    Member
    Posted 3 years ago #

    Hello, I'm just starting to play with filter hooks and in this instance trying to insert some elements after the post text. The problem that I faced is that all text will render after the post content, but the two functions (get_template_part and similar_posts) that I'm calling would actually render before the post content. This is the code that I'm using in functions.php (you can see how it looks at balticgamers.net/1/):

    function insertFootNote($content) {
            if(!is_feed() && !is_home()) {
    				//Starting FaceBook button code
    				get_template_part( 'template-part-FaceBook-button-bottom' );
    
    				//Starting related articles list
    				$content.= "<div id='similar-articles'>";
    				$content.= "<strong>Related Posts:</strong>";
    				$content.= "<br /><br />";
    				similar_posts();
    				$content.= "</div>";
    
    				//Starting copyright text
                    $content.= "<div id='terms-of-use'>";
                    $content.= "<a href='http://balticgamers.com/terms-of-use' title='Terms of use'>© copyright text »</a>";
                    $content.= "</div>";
            }
            return $content;
    }
    add_filter ('the_content', 'insertFootNote');

    Thank you!

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Since both get_template_part() and similar_posts() both seem to echo their results to the screen, you probably need use output buffering to capture the output and append the results of those functions to $content:

    ob_start();
    get_template_part( 'template-part-FaceBook-button-bottom' );
    $part = ob_get_clean();
    $content .= $part;
  3. Tomas Mackevicius
    Member
    Posted 3 years ago #

    Thank You, vtxyzzy !!! That worked!

    At my current knowledge level I would never get to this :)

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic