• Resolved Tim Burkart

    (@bigmoxy)


    I would like to display custom content at the end of each post on a blog page. I am hoping there is a WordPress hook for that. Can someone guide me in the right direction?
    Thank you!

Viewing 12 replies - 1 through 12 (of 12 total)
  • lisa

    (@contentiskey)

    Are you trying to add the custom content in a Full Site Editing theme or something else?

    Thread Starter Tim Burkart

    (@bigmoxy)

    The custom content will be added to the end of the post on the front end. This will be for the end user (site visitor). There is a separate function for managing the custom content.

    You could use the the_content filter. That will let you add in more content after the main post body.

    Thread Starter Tim Burkart

    (@bigmoxy)

    I was testing with the_post hook but it added my custom content to the beginning of the post. I want my content to appear after the post.

    Will the_content filter give me the post object? I need to query some of the post object fields?

    Looking at the documentation for it, it doesn’t give you the post object.

    But this is easy to work around. You can either use get_post() which will get the current post, or set global $post; at the top of your function.

    Thread Starter Tim Burkart

    (@bigmoxy)

    OK! It’s working fine on my blog page and single posts however when I go to a page, the content is not formatted and shortcodes are not rendered.

    Ideas or recommendations?

    It’s hard to say without seeing the code that you’re using. It could be a number of reasons.

    Thread Starter Tim Burkart

    (@bigmoxy)

    From what I can see there you’re using the filter wrong. The filter is meant to modify the content, and return that modified value, not output it. I have a feeling that what you’re seeing is because any content that you’re producing there doesn’t get processed like the rest of the content does.

    I’m also not a big fan of using goto statements in PHP (personal thing) but if that works for you I wouldn’t change it.

    Thread Starter Tim Burkart

    (@bigmoxy)

    I agree on the goto statements but for now that’s what I have.

    Can you help me and show me how I should do this? All I want to do is display a comment summary at the end of each post on my blog page.

    Thanks.

    The big thing is that you need to store the HTML in a variable and then return it at the end of the function. As a very basic example…

    add_filter ('the_content', 'my_content_filter');
    
    function my_content_filter ($content) {
        $extra_html = '<p>Here is my extra HTML</p>';
        return $content.$extra_html;
    }

    You can also set up object caching to do the same thing if you want to use outputs…

    add_filter ('the_content', 'my_content_filter');
    
    function my_content_filter ($content){
        ob_start ();
        ?>
            <p>Extra content here.</p>
        <?php
        $extra_html = ob_get_contents ();
        ob_end_clean ();
    
        return $content.$extra_html;
    }

    But being honest, if it was me I’d set up a child theme and modify the templates that are used to display posts, pages, etc, to add in that after the content is displayed. You can still call a central function for that, but you won’t need to worry about doing anything else.

    Thread Starter Tim Burkart

    (@bigmoxy)

    Got it! Thank you very much!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘How to display custom content at the end of post?’ is closed to new replies.