0

I'm getting custom fields using

<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
    <?php echo $key.': '.$value[0].'<br />'; ?>
<?php } ?>

and it is showing

_edit_last: 1<br>
_edit_lock: 1483226440:1<br>
_wp_page_template: page-services.php<br>
Body Repair: ValueBodyRepair<br>
_visual-subtitle: <br>

I need only 4th row Body Repair: ValueBodyRepair<br>

1

You should replace your call to get_post_custom with get_post_meta.

get_post_custom retrieves all the post meta fields whereas with get_post_meta you can specify the key of the meta field you want.

$meta = get_post_meta( $level_2->ID, 'Body Repair' );

This would give you an array with the value for the Body Repair meta key, To return a single value, you can use:

$metal key = YOUR META KEY
$meta = get_post_meta( $level_2->ID, $meta_key, true );
  • Thanks, but this is showing me only Body Repair: ValueBodyRepair<br> . There is array and can be any other value except Body Repair. I shouldn't mention Body Repair. – w3debugger Jan 1 '17 at 9:33
  • Then run a loop, passing the meta key in place of 'Body Repair' – Tunji Jan 1 '17 at 9:40
  • I've updated my answer – Tunji Jan 1 '17 at 9:42
  • <?php $meta = get_post_custom($level_2->ID); ?> <?php foreach ( $meta as $key => $value) { ?> <?php if(substr($key, 0, 1) !== '_'): ?> <?php echo $key.': '.$value[0].'<br />'; ?> <?php endif; ?> <?php } ?> this was the solution – w3debugger Jan 1 '17 at 13:52

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.