1

I want to associate pages with other pages by using custom meta values.

For one page, I want to associate two other pages, so I create a custom meta value with the key Procedure and the value of the other page ID, one for each page.

Then I want to display these associated pages, as their page titles, but in a comma separated list:

<?php echo implode(', ',get_post_meta($patient_story->ID, 'Procedure', false)); ?>

This is nearly there, but it only displays the page IDs (as it's giving the direct values in from the custom meta entries). I can't seem to integrate get_the_title() without breaking the code.

What's the best way I can have it so that it will convert:

Meta Key           Meta Value
Procedure    =>    238
Procedure    =>    240

into 'Page title 1, Page title 2'

1

You need to get the title for each post ID in your array. The straightforward way is this:

$procedure_title_list = array();
$procedure_list = get_post_meta($patient_story->ID, 'Procedure', false);
foreach ( $procedure_list as $procedure ) {
    $procedure_title_list[] = get_the_title( $procedure );
}
echo implode( ', ', $procedure_title_list );
  • Brilliant, I was thinking it must have been something simple, thanks. – Lee Jul 26 '16 at 15:39

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.