I have the following code running in a plugin:

 add_filter('the_content','thousand_pay');

//Callback function
function thousand_pay($content)
{
    echo $content;

    if( !in_category( 'Stories') )

    {
        return;
    }
    ?>
<hr></hr>
[Some HTML]
<?php

    return
}

For some reason, on individual posts’ pages the HTML gets printed multiple times:

Bug where HTML is printed multiple times

Can anyone think of why this would be? I read here that I might have to add to the conditional to check is_singular() and is_main_query(), so it would look like if(!in_category('Stories') || !is_singular() || !is_main_query(), but that just seems to stop the HTML getting printed at all on a post page. Any ideas?

3 Answers
3

It is normal for content to be accessed multiple times. For example SEO plugins need to do this to access it and generate meta data from.

Also it is a filter hook. Filters should never echo anything to the page, they are meant to modify passed value and return it.

If you do want to do something at that point, but only within the loop then in_the_loop() is condition you need.

Leave a Reply

Your email address will not be published. Required fields are marked *