0

I have a function set up to place some standard text in each post for a Custom Post Type. However, within that standard text I want to pull some unique text from a few custom fields. Not sure what is wrong with my code:

function default_content($content) {
  global $post;
  if ($post->post_type == 'my-custom-post-type') {
    $content .= '<p style="text-align: center;"><strong>Custom Field Text here: <?php echo get_post_meta( get_the_ID(), \'custom-field-1\', true )</strong></p>
    <p style="text-align: center;"><a href="http://myblog.com/?checkout=<?php echo get_post_meta( get_the_ID(), \'custom-field-2\', true )">Link 01</a></p>';
  }
  return $content;
}
add_filter('the_content', 'default_content', 0);
0

You are trying to echo within a variable. You also have encapsulated PHP tags in the string as well. You need to concatenate just the functions themselves like this:

    function default_content($content) {
        global $post;

        if ($post->post_type == 'my-custom-post-type') {
            $content .= '<p style="text-align: center;"><strong>Custom Field Text here: '. get_post_meta( $post->ID, "custom-field-1", true ).'</strong></p>
                         <p style="text-align: center;"><a href="http://myblog.com/?checkout=' . get_post_meta( $post->ID, "custom-field-2", true ).'">Link 01</a></p>';
        }
        return $content;
    }
    add_filter('the_content', 'default_content', 0);

Also you are calling global $post, so use $post->ID instead of get_the_ID().

Cheers!

  • Perfect, thank you! I am slowly but surely getting the hang of this WordPress thing (I think)! Much appreciated on the help. – George C Sep 16 '11 at 5:04
  • No worries. :) That's why we're here. Everyone has to start somewhere. – Brian Fegter Sep 16 '11 at 5:06

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.