Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to use a filter for post content. But how do i make an IF work inside the function.

<?php function my_the_content_filter($content) {
  } add_filter( 'the_content', 'my_the_content_filter' ); ?>

So i need to add this inside of it.

<?php if (get_post_meta($post->ID, "heading_image", true)) : ?>  
      <img src="<?php $image_id = get_post_meta($post->ID, "heading_image", true);
     $post_image_data = wp_get_attachment_image_src( $image_id, $size='full' );
           echo $post_image_data[0]; ?>" style="max-width:1000px;" />
     <?php endif; ?>

I cant seem to format it correctly.

share|improve this question

closed as off-topic by s_ha_dum, t f, toscho Dec 18 '13 at 10:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – s_ha_dum, toscho
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

This should be it -

<?php function my_the_content_filter( $content )
{
    if( get_post_meta( get_the_ID(), "heading_image", true) )
    {
        $image_id = get_post_meta( get_the_ID(), "heading_image", true);
        $post_image_data = wp_get_attachment_image_src( $image_id, 'full' ); ?>

        // checks if you have got the source
        if( isset($post_image_data[0]) )
        {
            // if image is show before the content
            $content = '<img src="'. $post_image_data[0] .'" style="max-width:1000px;" />' . $content;
        }
    }

    return $content;
}
add_filter( 'the_content', 'my_the_content_filter' ); ?>
share|improve this answer

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