Support » Fixing WordPress » Hard Code Custom Field Value Into Template?

  • I would be so appreciative if someone could help me with the following. I’m trying to hard code the output of a custom field right into my template before the_content, so that each post begins with the custom field content (which are youtube urls).

    I’m trying to make it automatic so that I don’t have to manually go in and copy the youtube url from the custom field and paste it at the beginning of the post content.

    So far I’ve been able to output the custom value, but all I get is plain text youtube url…as opposed to having it automatically embed the video within the post as it does when you add a youtube url directly to a post.

    I’ve been playing around with these:

    <?php
           $var = apply_filters('the_content', get_post_meta($post->ID, 'My Custom Field') );
    ?>

    <?php echo get_post_meta($post->ID, “My Custom Field”, true); ?>`

    Does anyone have any advice or experience with this?

    Thank you,

    David

Viewing 8 replies - 1 through 8 (of 8 total)
  • When I use put the apply_filters code before the_content I am able to get the youtube video showing, but the rest of the post content is not displayed.

    <?php $mynewcontent = get_the_content();
    
    $mynewcontent = apply_filters('the_content', get_post_meta($post->ID, 'My Custom Field') );

    then echo out $mynewcontent….. maybe something like that? (I’m totally fresh to this stuff, trying to adapt from something I did)

    Hey Rev thanks for that, but it came up with some formatting error in functions.php. Good thought though…

    did you add that to functions.php??

    I would have just run it in the template in question

    No I added it right to my single.php file. Actually, right before the_content (in single.php) I include a file called video.php which has the 4 or so lines of code

    OK, looks like what you were working with may work….

    <?php
            $var = apply_filters('the_content', get_post_meta($post->ID, 'Custom Field', $single = true) );
    ?>

    in single.php and then, echo out $var I believe to show the video….

    so put that above code, and then echo out $var, and then your normal the_content

    Is that what you did? You never mentioned if you tried to echo out $var above

    so in total I guess…

    <?php
            $var = apply_filters('the_content', get_post_meta($post->ID, 'Custom Field', $single = true) );
            echo $var;
    ?>

    I believe where you want the vid

    Hey Rev, sorry I had to run out for an hour…Anwyay, when I put that above the_content that does work to get the video to appear. However, for some strange reason it leaves out the rest of the post content.

    When I put it below the_content, it begins with the post content and then the video appears. What I’m really shooting for is to start with the video and then have the post content.

    Just wanted to say that I figured it out and leave the code behind for anyone struggling to do the same. The following will extract your specified custom field and add it your post automatically:

    <?php
    	global $postID;
            $var = apply_filters('the_content', get_post_meta($post->ID, 'Your Custom Field', $single = true) );
    
    echo $var;
    
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Hard Code Custom Field Value Into Template?’ is closed to new replies.