0

I have a simple plugin which adds a signature at the end of each post. But it also appears after user comment section. How to avoid the latter?

Update

I have tried to run the function once by adding a static variable. Now signature is just adding to comment section. I need exactly the reverse. Hope this helps.

<?php
/*
header...
*/

if( !function_exists("add_signature")){
    function add_signature($content){

    /* code related to update
    static $once;

    if ( $once !== null )
            return $content;
        else
            $once = 'done';
    */

        if( !is_page() )
            return $content . "signature";
    }
    add_filter('the_content', 'add_signature');
}

?>
| improve this question | | | | |
  • From your code sample you are appending it to the the_content() function. If your comments template is using the_content() in any way this could be why its duplicating. If you want it to appear on every post page, I would build it into the template rather than appending it to the_content(). – EBennett Oct 3 '16 at 7:55
  • @EBennett: If that's the case i should avoid adding signature when comment section is already added to content. I 've tried checking strpos($content, 'comment_form') == false before adding signature but still signature is printed twice. – Xaqron Oct 3 '16 at 16:19
  • I have tried other themes like the built-in ones and problem didn't solve. – Xaqron Oct 3 '16 at 16:25
  • Could you update your example to include code for your template files where this is happening? This will allow for a better understanding of how you are using this code. – EBennett Oct 4 '16 at 11:43
0

You can use get_post_type() to check which content you're appending your signature to.

function add_signature($content){
    if( get_post_type() == 'post' )
        return $content . "signature";
}
add_filter('the_content', 'add_signature');
| improve this answer | | | | |
  • still twice. on at the end of post and one after comment section – Xaqron Oct 3 '16 at 7:07
0
add_filter( 'the_content', 'add_signature' );

function add_signature( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() &&  get_post_type() == 'post' ) {
        return $content .  "signature";
    }

    return $content;
}
| improve this answer | | | | |
  • 1
    Please explain the code in additon to posting it. – bravokeyl Oct 3 '16 at 8:16
  • with this constrain applied, nothing is added neither to post nor to comment section because in_the_loop() is always false in my case. – Xaqron Oct 3 '16 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.