1

I have created a meta box that allows users to add an additional name/id to their post. From what I can tell it is adding the meta information correctly because when I edit a post that has a custom name/ID it shows up in the input box by default as it should.

So my issue is that I am trying to pull the custom ID/name and use it as an additional ID tag in the code. Although it is not retrieving anything. When I use the code in my theme and check the html on the page the ID is showing ID="". So for some reason it is not pulling anything. Can anyone see an issue?

<?php $post_id = get_the_ID(); ?>
    <?php $value = get_post_meta($post_id, 'newtheme_section_id'); ?>
    <id="<?php $value; ?>">

If I change get_the_ID() to the_ID() then it returns a value but it is the post ID instead of the custom id the user entered into the feild.

EDIT: I was able to figure it out by echoing the function instead of storing it in a variable, along with Mannys suggestion . Here is the updated code.

<?php $post_id = get_the_ID(); ?>
<id="<?php echo esc_attr( get_post_meta($post_id, 'newtheme_section_id', true) ); ?>">

1 Answer 1

0

Make sure you include the third argument to get_post_meta, a Boolean that determines whether or not it returns a single item or an array of items. The default is false, which is an array, so set it to true:

  <?php $value = get_post_meta ($post_id, 'newtheme_section_id', true ); ?>
2
  • Thanks, I added it but unfortunately I am still having the same issue. Commented Apr 21, 2013 at 4:02
  • I was able to echo the function instead of storing it(output) in a variable. I had tried this before your suggestion and it did not work, so I am marking your post as the answer. Commented Apr 21, 2013 at 4:15

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.