WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding get meta data to a loop (4 posts)

  1. martcol
    Member
    Posted 1 year ago #

    If I add either of the following statments in the loop it works out OK

    echo get_post_meta($post->ID, 'phone', true);
    
    $key2="phone"; echo get_post_meta($post->ID, $key2, true);

    If I add this in the same place it goes pear-shaped and gives me an error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'

    $phone = get_post_meta($post->ID, 'phone', true);
    if ($phone) {
    echo '<div class="sideBox">'$phone'</div>';
    }

    Can someone please try and tell me why the second option doesn't work. I guess you can see that I want to call this custom field meta data but wrap it in some HTML

    Thanks

    Martin

  2. Justin
    Member
    Posted 1 year ago #

    $phone = get_post_meta($post->ID, 'phone', true);
    if ($phone) {
    echo '<div class="sideBox">'.$phone.'</div>';
    }

    You are missing the periods in the echo statement

  3. martcol
    Member
    Posted 1 year ago #

    Justin, thank you for that. I can't try it out until the morning Zzzzzzz

    Can I just ask why it doesn't work without the period? Is that a concatenation thing and if it is, is it like sticking things together or keeping them apart?

    Thank again, I can't wait to give it a go

    Martin

  4. martcol
    Member
    Posted 1 year ago #

    Terrific!

Topic Closed

This topic has been closed to new replies.

About this Topic