1

Not sure if this is a WordPress issue, but I don't think so.

I have a while loop where I'm iterating over a user-created loop of content and trying to get a meta field, which has a numeric identifier which matches the current "key" (if you can call it that, since this is not an array at all, just a custom loop the user can create with some front-end parameters. $this_label_url is requesting the faux "key" via the interpolation with $this_label_url = get_post_meta($post->ID, 'instructor_{$number}_label_url', true);. Is variable interpolation not possible in this scenario?

<?php
   $total_panels = get_post_meta( $post->ID, 'total_panels', true );  // set by the user with a custom meta value called "total_panels" (an integer)
   $count = 1; // just a fake "key" for looping through my `while`

   // create a faux "number" - simply takes $count and adds leading zero if not present
   while ( $total_panels >= $count ) :

    if ( $count >= 9 ) {
        $number = '0' . $count;
    } else {
        $number = $count;
    } 
$this_label_url = get_post_meta($post->ID, 'instructor_{$number}_label_url', true);  // returns as an empty string, 
1
  • 2
    Change the single quotes ' to double quotes " Commented Oct 30, 2012 at 3:13

1 Answer 1

2

It is possible, but you need to use double quotes; single quotes aren't parsed for interpolation.

$this_label_url = get_post_meta($post->ID, "instructor_{$number}_label_url", true);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - one of those annoying "gotcha's". I guess I'm a terrible person for preferring single quotes, I knew it would catch up with me some day.
@Brian There is a time and place for them, like when you want a literal $ in your string without escaping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.