Support » Plugins » Hacks » Set Post Meta with A Variable

  • Hi,

    I am using the following code to set an image to be displayed. With a meta tag that is name after a variable. Problem is it does not pick up the variable value when I add it to the get post meta.

    $attachment_id2 = get_post_meta ($post->ID, 'special_feature_'.$special_feature1, $single = true);}

    Any Idea on how to make sure the variable is added to the meta value.

Viewing 1 replies (of 1 total)
  • I think the way you are using the key (variable + string) is messing up the function call. If you have to do it this way, I would take out ‘special_feature_’ and make it a variable like $sf_txt = ‘special_feature_’. Also, instead of naming each variable with a number, use an array – much more efficient and less code. I couldn’t help but write a little for you quickly.

    $features = array('feature1', 'feature2', 'feature3');
    $sf_txt = 'special_feature_';
    for($i = 0; $i < count($features); $i++){
    $sp_feat = $sf_txt.$features[$i];
    $attachment.$i = get_post_meta ($post->ID, $sp_feat , $single = true);

    A foreach might be more useful in your case, I can't see all your code though and dont know what queries you're using.

Viewing 1 replies (of 1 total)
  • The topic ‘Set Post Meta with A Variable’ is closed to new replies.