0

I am using a child theme and try to get some code show up after the content on single post/pages

function app_bits() {
global $post;
$app_logo = get_post_meta($post->ID, 'AppLogo', TRUE);
$zund_id = get_post_meta($post->ID, 'AppURL', TRUE);
if ( is_single() || is_page() ) {
?>
<div class="hidemobile">
<div class="one_half">
<!--  ad code  -->
sample
</div>
<div class="one_half column-last">
<?php
if ($zund_id !='') {
?>
<div style="text-align: center;">custom code goes here
</div>
<?php } else { ?>
<div style="text-align: center;">custom code goes here
</div>
<?php } ?>
</div>
</div>
<?php
    }
}
add_filter( 'the_content', 'app_bits');

The issue is it replaces the content. Is there a after_the_content hook I can use so that the content I want goes after the content?

After reading what @toscho said that I came up with this ( I am not some one who uses PHP , which should become obvious after looking at the code below -_- ) if anyone esle is looking for the same , here is what I used

function app_bits($content) {
global $post;
$app_logo = get_post_meta($post->ID, 'AppLogo', TRUE);
$zund_id = get_post_meta($post->ID, 'AppURL', TRUE);
if ( !is_singular() ) 
{
return $content;
}
$custom_bits1='<div class="hidemobile">
<div class="one_half">
<!--  ad code  -->
sample here
</div>
<div class="one_half column-last">';
$custom_bits2='<div style="text-align: center;">custom code goes here
</div></div></div>';
$custom_bits3='<div style="text-align: center;">custom code goes here
</div></div></div>';
if ($zund_id !='') {
return $content . $custom_bits1. $custom_bits2;
} else {
return $content . $custom_bits1. $custom_bits3;
}
}
add_filter( 'the_content', 'app_bits');
3
  • 1
    you should not echo the content through the function, you should return it. Or if you want to use echo, take a look at output control functions in php Commented Oct 19, 2012 at 11:28
  • @MridulAggarwal In such cases where a comment fixes an issue, it should've been an answer instead and followed by a short example - go for rep points, man!
    – kaiser
    Commented Oct 19, 2012 at 11:36
  • @kaiser I'll do that from next time, but this time toscho answered first, so i'll pass Commented Oct 19, 2012 at 11:52

1 Answer 1

2

You get the $content as a parameter for your function, and you should not echo anything in that function. Return a string instead.

Example:

add_filter( 'the_content', 'app_bits');

function app_bits( $content )
{
    if ( ! is_singular() ) 
    {
        return $content;
    }

    $extra = '<div style="text-align: center;">custom code goes here</div>';

    return $content . $extra;
}

See How to return loop contents for an example showing how to return a rather complex string with markup.

1
  • small hint for the conditional statemant, the is_preview is often forgotten, but usefull.
    – bueltge
    Commented Oct 19, 2012 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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