0

I'm not sure where I am going wrong with this code. I'm simply trying to output an array from a variable into it's separate items.

For example:

$current_endMaps = get_post_meta($post->ID, "_tsb_postmeta_end_maps", true);

Will give me the following output...

["http://localhost/lmn-beta/wp-content/uploads/2017/10/image1.png","http://localhost/lmn-beta/wp-content/uploads/2017/10/image2.png"]

But I am trying to output this as follows...

http://localhost/lmn-beta/wp-content/uploads/2017/10/image1.png
http://localhost/lmn-beta/wp-content/uploads/2017/10/image2.png

Here is what I have so far... but this is only outputting this

[

Here is the code I am using..

$current_endMaps = get_post_meta($post->ID, "_tsb_postmeta_end_maps", true);
$arrlength = count($current_endMaps);
for($x = 0; $x < $arrlength; $x++) {
    echo $current_endMaps[$x];
    echo "<br>";
}

Any help would be greatly appreciated.

6
  • Is it not working? There is a simpler way to loop an array in PHP, but this should nevertheless be working. Commented Oct 28, 2017 at 23:06
  • Whats the easier way? I'm assuming that something in wordpress is just messing up the output. Commented Oct 28, 2017 at 23:07
  • The [ output is suspicious. View the page source of the resultant output. Do you see additional stuff in there, like broken, non-rendered HTML? Instead of echo $current_endMaps[$x] debug with var_dump($current_endMals[$x]. Commented Oct 28, 2017 at 23:09
  • Also, incrementing for () loops are rarely used in PHP when iterating an array. This shouldn't affect your output, but instead I'd recommend foreach($current_endMaps as $current_endMap) {...} and access inside simply as echo $current_endMap Commented Oct 28, 2017 at 23:10
  • I don't know. Something must be wrong because I've done this many times before but the output is either the same or showing errors. Commented Oct 28, 2017 at 23:20

2 Answers 2

3

I believe you need to supply false as the third argument to get_post_meta() to get an array

see https://developer.wordpress.org/reference/functions/get_post_meta/

$single (bool) (Optional) Whether to return a single value. Default value: false

Sign up to request clarification or add additional context in comments.

Comments

0

Found out what I was doing wrong...

Just added json_decode to the initial array...

$current_endMaps = json_decode(get_post_meta( $post->ID, "_tsb_postmeta_end_maps", true ));
$arrlength = count($current_endMaps);
for($x = 0; $x < $arrlength; $x++) {
    echo "<img src='".$current_endMaps[$x]."' width='100%'>";
}

Then finished the echo output as an image. Works now.

Comments

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.