Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

PHP manipulation of list of image URLs in a custom field WordPress

  • SOLVED

I'm trying to use 1 custom field for a bunch of images - to do the multiple things to all the images. I can store them in the custom field however is advisable, but I thought this format would be best, since I think that's what a PHP array goes into:
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image3-Th.jpg'


So, once I have my custom field values entered for a post, here's my non-working PHP code:
<?php //og images
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
global $wp_query; $postID = $wp_query->post->ID;
$photosfull = array(get_post_meta($postID, 'custom_field_name', true));
echo $ogimagepre.$photosfull.$ogimagepost
?>


You can see I'm trying to get this result:
<meta property="og:image" content="http://images.domain.com/image1-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image2-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image3-Th.jpg"/>


That's 1 thing I'd liked to do with this list of images. Ideally, I'd be able to do other things using the same array. Such as replace "-Th.jpg" with "-X3.jpg", since that's a larger size of the same image. And other stuff as I think of it.

The entire point is to not have to have 10 custom fields if I have 10 images. That wouldn't be manageable and it would be more (unnecessary) database calls.

Originally posted here: [[LINK href="http://stackoverflow.com/questions/7057207/easy-php-array-of-images-from-a-wordpress-custom-field"]]http://stackoverflow.com/questions/7057207/easy-php-array-of-images-from-a-wordpress-custom-field[[/LINK]]


QUESTIONS:
1) Is this " single-quote, URL, single-quote, comma, line-break " the recommended method for the custom field value?
2) The PHP code for manipulating the field values as requested.
3) Any other suggestions?


Thanks!

Answers (4)

2011-08-16

Kailey Lampert answers:

What
$photosfull = array(get_post_meta($postID, 'custom_field_name', true));
is effectively getting you right now is this:
array( "'http://images.domain.com/image1-Th.jpg', 'http://images.domain.com/image1-Th.jpg', 'http://images.domain.com/image3-Th.jpg'" );

not
array( 'http://images.domain.com/image1-Th.jpg', 'http://images.domain.com/image1-Th.jpg', 'http://images.domain.com/image3-Th.jpg' );


What might be more effective is to add your images to the custom field in this format
http://images.domain.com/image1-Th.jpg,
http://images.domain.com/image1-Th.jpg,
http://images.domain.com/image3-Th.jpg


Then do this
$photosfull = explode( ',', array(get_post_meta($postID, 'custom_field_name', true)));
foreach ( $photosfull as $imgurl ) {
echo "{$ogimagepre}{$imgurl}{$ogimagepost}\n";
}
(code edited to correct typo)


Clifford P comments:

I was afraid of the "not really a PHP array" issue. Thanks for putting it so plainly.

I tried your code (explode instead of expode) and the HTML output was simply this:
<meta property="og:image" content="Array"/>

I got that same issue with some other code I tried several iterations ago. Not sure where to go from here.

One question is whether the image URLs should be posted as " URL comma line-break ". That's what I put in for the custom field I tested your script on.


Kailey Lampert comments:

Oops, sorry 'bout that typo :)

Is your echo inside the foreach() loop?

what do you get if you add this:

echo '<pre>' . print_r( $photosfull, true ) .'</pre>';<code>

just after the <code>$photosfull = explode( ',', array(get_post_meta($postID, 'custom_field_name', true)));

line?


Kailey Lampert comments:

</code></code>
Whoa - did I just break the page?

hopefully you can still follow what I was trying to say


Clifford P comments:

I see a box with your name, then an empty box, then a box with you asking if I could see what you wrote, which I cannot - the empty box I presume. Please try again. :)


Kailey Lampert comments:

That's what you get when you miss the slash in a closing tag...

here it is again:

Is your echo inside the foreach() loop?

what do you get if you add this:

echo '<pre>' . print_r( $photosfull, true ) .'</pre>';

just after the
$photosfull = explode( ',', array(get_post_meta($postID, 'custom_field_name', true)));

line?


Clifford P comments:

Oh, now I see the box above those 3 boxes I mentioned.

Since this og:image stuff is for Facebook in the <head></head> section, I'm guessing I don't want/need the <pre> tag. Assuming that's just for testing...

The code:
<?php //og images
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
$photosfull = explode( ',', array(get_post_meta $postID, 'tko_photos_full_screen_array_noquotes', true)));
echo '<pre>' . print_r( $photosfull, true ) .'</pre>';
foreach ( $photosfull as $imgurl ) {
echo "{$ogimagepre}{$imgurl}{$ogimagepost}\n";
}
?>


The output:
<pre>Array
(
[0] => Array
)
</pre><meta property="og:image" content="Array"/>


Kailey Lampert comments:

Yes pre tags just for testing, out of habit.

Ok, I think I see my mistake, remove the array portion (and it looks like we may have lost a parenthesis somewhere along the way)

$photosfull = explode( ',', get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true));


Kailey Lampert comments:

and there should be a semi-colon at the end of that line too


Clifford P comments:

Code:
<?php //og images
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
$photosfull = explode( ',', get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true));
echo '<pre>' . print_r( $photosfull, true ) .'</pre>';
foreach ( $photosfull as $imgurl ) {
echo "{$ogimagepre}{$imgurl}{$ogimagepost}\n";
}
?>



Result (in <head> section):
<pre>Array
(
[0] =>
)
</pre><meta property="og:image" content=""/>


Kailey Lampert comments:

I'm starting to run out of ideas...

If you remove the explode portion, can you confirm that there is actually data?

$photosfull = get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true);
print_r( $photosfull );


Clifford P comments:

Code:
<?php //og images
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
$photosfull = get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true);
print_r( $photosfull );
?>


There is no output.

Is that what you were expecting?
Maybe the title of this question shouldn't have the word "easy"... sorry.


Kailey Lampert comments:

That explains why the array was empty when printed.

Some how, the custom field 'tko_photos_full_screen_array_noquotes' for the post ID you provided is empty/nonexistent.

Maybe the $postID variable is empty? You can try printing that:

print_r( $postID );


Clifford P comments:

Yup, postID wasn't there either.

I'm guessing something like this needs to be added somewhere, but I tried putting it in several places and I either got an error or an empty array again:
global $wp_query; $postID = $wp_query->post->ID;


Kailey Lampert comments:

Alright, try this:
<?php //og images

if (is_single()) {

global $post;
$postID = $post->ID;

$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
$photosfull = explode( ',', get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true));
foreach ( $photosfull as $imgurl ) {
echo "{$ogimagepre}{$imgurl}{$ogimagepost}\n";
}

}
?>


Clifford P comments:

No output. If we get this figured out, I'll increase the prize...


Kailey Lampert comments:

I'm assuming these tags are going on a 'single' view (a page/post versus an archive or multi-post view). You can try commenting out the is_single() conditional.

And/or try printing out $postID and $photosfull again, see if the data you're expecting is actually being retrieved.

If you were willing to give me access (wp-admin and ftp), I'd be happy to to take a look at the actual code - might speed things up a bit :)


Kailey Lampert comments:

Just made a small tweak, using 'is_singular' instead of 'is_single' (I made an assumption about posts vs page)

Appears to be working now, take a look.


Clifford P comments:

Excellent. Here's your code that's working:
<?php //og images
// global $wp_query; $postID = $wp_query->post->ID; echo get_post_meta($postID, 'tko_photos_open_graph_meta', true);
if (is_singular()) {

global $post;
$postID = $post->ID;
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
$photosfull = explode( ',', get_post_meta( $postID, 'tko_photos_full_screen_array_noquotes', true));
$photosfull = array_map( 'trim', $photosfull); //remove surrounding whitespace
if (is_array($photosfull)) {
//check that there is data to output, otherwise there will be pointless meta tags
foreach ( $photosfull as $imgurl ) {
echo "{$ogimagepre}{$imgurl}{$ogimagepost}\n";
}
}

}
?>


As a bonus...

Assuming I'm using just 1 custom field to do everything relating to images, could you make it so -X3.jpg is replaced with -Th.jpg in the image URLs for og:image?


Kailey Lampert comments:

Add this inside the foreach, and above the echo

$imgurl = str_replace( '-X3', '-Th', $imgurl );
$imgurl = str_replace( '/X3/', '/Th/', $imgurl );


But since I had the file open, I've added it for you. I'll close it now and won't open it again unless you ask (don't want to risk overwriting anything).


Clifford P comments:

Bravo. Great job. Thanks!
I'm looking forward to Jermaine's reply, since his code looks a bit more compact.

Do you have any suggestions if I change to using a custom write panel (i.e. meta_box) instead of custom_field? Would your code have to be edited?

:)


Kailey Lampert comments:

If you switched to a meta box, the contents would still be stored in a custom field, so they'll be accessed just the same - might just have to adjust the name of the field.


Clifford P comments:

Please look at my code now and tell me what's wrong - why it's not printing the og:image fields correctly. I feel like I'm pretty close to my ideal solution. Thanks!


Clifford P comments:

And, since it's working, I'm guessing these 2 codes do the same???
global $post;
$postID = $post->ID;

global $wp_query;
$postID = $wp_query->post->ID;


Pretty nifty.


Is it best practice to always include [[LINK href="http://codex.wordpress.org/Function_Reference/wp_reset_query"]]wp_reset_query();[[/LINK]] after code like that?


Kailey Lampert comments:

Looks like
global $wp_query;
$postid = $wp_query->post->ID;

is actually preferred, but I'm not sure exactly why.

wp_reset_query() should only be necessary if you're creating a custom/new Loop (with query_posts() or WP_Query()).

If you wanted to do as Jermaine mentioned and not use commas, just new lines you could do this:

$images = get_post_meta( $post->ID,‘Images’, true );
$images = explode( "\n",$images );


Clifford P comments:

I took a closer look at Jermaine's offered solution and I realized it was the same code functionality, just less lines. I prefer more lines so I'm sticking with your code version.

Did you also notice this reply that I made?
<blockquote>08/17/11 5:36am
Cliff P says:
Please look at my code now and tell me what's wrong - why it's not printing the og:image fields correctly. I feel like I'm pretty close to my ideal solution. Thanks!
</blockquote>


Kailey Lampert comments:

I most have skipped over that one...

I'm just heading out the door for a couple hours, so I won't be able to take a look till I get back.


Clifford P comments:

I look forward to your reply/solution. Thanks.


Kailey Lampert comments:

I've made some adjustments, take a look and let me know how close it is


Clifford P comments:

Yes, that worked. I kind of got sidetracked (working on this as I find a few spare minutes in the day). Could you look at the $addressfull that I created?

I'll be able to check back here in 1 hour at the earliest, but you can take longer if needed, of course. Just saying I'll be out a bit.

Thanks.


Kailey Lampert comments:

Semi-colons :)

page no longer errors-out and formatted address can be seen in the source code


Clifford P comments:

Doh! Thanks.

Okay, last thing for now (I hope)... I was hoping to replicate the first solution's code but I realize it's a bit different than what I expected.

I know it's not correct code so I didn't save it anywhere, but here's my mock-up that should be good enough for you to understand what I want:
<?php // Galleria Display
if ( !is_null($imagesfull) || !empty($imagesfull)) { // if fullsize images exist
// the static wrapping text (i.e. not an array)
$galleria1 = '<a rel="';
$galleria2 = '" href="';
$galleria3 = '"><img src="';
$galleria4 = '" alt="';
$galleria5 = '" title="';
$galleria6 = '"></a><br />';
// do the cool stuff: create variable that would output text like is currently on my html code for the photo display
if (is_array($imagesfull)) {
foreach ( $imagesfull as $imgurl ) {
$galleriabrandedarray = "{$galleria1}{$imagesfull}{$galleria2}{$imagesmain}{$galleria3}{$imagestinythumbnails}{$galleria4}{$addressfull}{$galleria5}{$agentcommabroker}{$galleria6}";
$galleriaunbrandedarray = "{$galleria1}{$imagesfull}{$galleria2}{$imagesmain}{$galleria3}{$imagestinythumbnails}{$galleria4}{$addressfull}{$galleria5}{$galleria6}"; // no $agentcommabroker
}
}
// display branded images
?>
<div id="photos" class="branded"><h2>Photos</h2>
<?php echo do_shortcode('[Photo_Display_Tips]'); ?><br/>
<div id="galleria" class="branded" style="text-align:center;"><?php echo $galleriabrandedarray ; ?></div></div>
<?php // display unbranded images ?>
<div id="photos" class="unbranded"><h2>Photos</h2>
<?php echo do_shortcode('[Photo_Display_Tips]'); ?><br/>
<div id="galleria" class="unbranded" style="text-align:center;"><?php echo $galleriaunbrandedarray ; ?></div></div><?php
<?php }
?>


The current code that creates the images now is this (tour.php):
<?php // Galleria Photo Display
if( get_post_meta($post->ID, 'tko_photos_branded_galleria', true) ):?>
<div id="photos" class="branded"><h2>Photos</h2>
<?php echo do_shortcode('[Photo_Display_Tips]'); ?><br/>
<div id="galleria" class="branded" style="text-align:center;"><?php echo get_post_meta($post->ID, 'tko_photos_branded_galleria', true); ?></div></div><?php
else:
endif;?>


The reason I don't want to use that is because I want to keep using that 1 field for the list of images, instead of having another field specific to the photo display section.

Thanks.


Kailey Lampert comments:

changes made - let me know.


Clifford P comments:

Great - almost right.

I changed it to this:
$ahref = $imagesmain[$k];

And added $imagesmain a few lines above that:
global $addressfull, $agentcommabroker, $imagesfull, $imagesmain, $imagessmall, $imageslargethumbnails, $imagestinythumbnails;

But href="" if you view source of page.


Kailey Lampert comments:

Has to be made global in header.php too (I've just added it there)


Clifford P comments:

Great, thanks.

Can the HTML/view source look prettier - each on its own line, like it was before?

Do you have any commentary on Jermaine's concerns? Could the code be more bullet-proof, so to speak?


Kailey Lampert comments:

I've added some line breaks to make the html source slightly easier to read - but since the image names are so long, the code may wrap anyway (in some browsers).

I've also removed a few redundant checks to make the php a bit easier. (e.g. we don't need to make sure the variable is set, and is not empty, and is an array)

I'm not sure what you mean by making it more bullet-proof. More efficient? Or doing things like verifying that the image really is an image before outputting it?


Clifford P comments:

Kailey, you've been great and obviously your code is working. The bullet-proof word was coming from: if Jermaine was right about nl2br() or other things, let's get the best of both solutions.

IE9 shows <br /> after each image line in view source. When I used a separate custom field for the Galleria display images, (<a rel href src /> all in one line, each image on each line) there was no <br /> in the view source. Could an \n or something else be added instead - something that wouldn't even be HTML output?

The conditionals are because not every post like this (i.e. listing) has photos - some only have video. So I don't want the <h2>Photos</h2> and photo help text and whatnot if no photos exist for the post. Is that what you took out?


Kailey Lampert comments:

I'm really not sure what the issue is with using '\n'. I've never had a problem using the explode function with that delimiter. If you wanted to, you could use the nl2br function and explode by <br /> but since the code is working as it, I don't see how this would be advantageous.

There is a \n after the </a> tag.
<a><img /></a><br />\n
If you don't what the <br /> tags in the source, you can remove them from lines 92 and 94 of tour.php (they were added because you had them in $galleria6 = '"></a><br />';

The only conditionals I removed had to do with making sure that the images array was populated.

Originally, there was this (comments removed):
if ( !is_null($imagesfull) || !empty($imagesfull)) :
$galleriabrandedarray = '';
$galleriaunbrandedarray = '';
if (is_array($imagesfull)) {
foreach ( $imagesfull as $k => $imgurl ) {


which I've consolidated down to this:
if ( is_array($imagesfull) ) :
$galleriabrandedarray = '';
$galleriaunbrandedarray = '';
foreach ( $imagesfull as $k => $imgurl ) {


Clifford P comments:

glad to hear you didn't change the other things. what you changed is fine with me if it's fine with you.

the \n is working obviously.

I removed my own <br />. ;)

---

You see the branded photos and the unbranded photos?
Why is there a big gap before "1/87" on the first set but not the second?

I've never had 2 Galleria displays on a page before, but when I had just 1 Galleria display, it had no space until I did the WP enqueue script coding - then had the big gap. Now that I have enqueue script and 2 displays, the top display has the gap and the bottom display does not. I would really prefer if no displays had the gap, but it's not life-or-death. Just wondering if you could offer that additional help.

Thanks.


Kailey Lampert comments:

The first galleria container opens like this:
<div id="galleria" class="galleria branded" style="text-align:center;">
And the second like this
<div id="photos" class="galleria unbranded">

copy the text-align: center style to the second and you should be good to go

By the way, you have at least 2 different IDs that are duplicated (id="photos" and id="galleria") on this page, IDs are not meant to be repeated, so there could be issues because of this. For example, the galleria was initially targeting the galleria ID, so it couldn't work on both sets of photos. I've since updated (and noted in the code) that for you (changed to target a class of galleria), but there could be other issues.


Clifford P comments:

Fixed. Wow, I like you. :)

You respond so quickly and fully explain everything; I feel like we're hanging out in the next room from each other. Get out of my house! j/k

Thanks for the great, great, great help. I know you didn't get rich off me, but I really appreciate it and hope it was worth your effort.

Ciao for now!

2011-08-16

Jermaine Oppong answers:

I wrote a post a while ago on [[LINK href="http://www.graphicbeacon.com/web-design-development/how-to-split-and-categorize-wordpress-custom-field-values/"]]How to Split and Categorize WordPress’ Custom Field values[[/LINK]] addressing this.

You can then adjust the <strong>foreach</strong> function into this:


foreach($images as $src)
{
echo "<meta property=\"og:image\" content=\"".trim($src)."\" />\n";
}



Clifford P comments:

I skimmed and really liked your blog post. Thank you for the link.

Could you repost your code in its entirety, including opening and closing PHP, to make sure I'm not missing something? (P.S. Remember, this is outside the Loop).

Thanks!


Clifford P comments:

I also noticed your blog example required line breaks, but no quotes or commas. Do you suggest not using the commas (I already took out the single-quotes)?


Jermaine Oppong comments:

You do not need quotes or commas in your custom field - [[LINK href="http://www.graphicbeacon.com/wp-content/uploads/2010/09/customfieldthumbs_thumb.jpg"]]simply have the urls on a single line[[/LINK]]

And then use this code:


<?php

/* Grab Custom field 'Images' value and split into array */
$images = get_post_meta($post->ID,‘Images’);
$images = nl2br($images[0]);
$images = explode(‘<br />’,$images);

foreach($images as $src){
echo "<meta property=\"og:image\" content=\"".trim($src)."\" />\n";
}

?>


This will grab the urls from the Custom Field called 'Images', split the lines and echo the way you want it. Thats pretty much it :-)


Jermaine Oppong comments:

Actually you may need this instead for outside the loop:


<?php

global $wp_query;
$postid = $wp_query->post->ID;

/* Grab Custom field 'Images' value and split into array */

$images = get_post_meta($postid,‘Images’);
$images = nl2br($images[0]);
$images = explode(‘<br />’,$images);

foreach($images as $src){
echo "<meta property=\"og:image\" content=\"".trim($src)."\" />\n";
}


?>



If the previous dont work then this should.


Jermaine Oppong comments:

If you want this to work only on a single post page then add an if statement to check if this is a single post page:




<?php

global $wp_query;
$postid = $wp_query->post->ID;

if(is_single()){

/* Grab Custom field 'Images' value and split into array */
$images = get_post_meta($postid,‘Images’);
$images = nl2br($images[0]);
$images = explode(‘<br />’,$images);


foreach($images as $src){
echo "<meta property=\"og:image\" content=\"".trim($src)."\" />\n";
}

}// end if

?>



Use the previous code if you want it to work on all pages.


Clifford P comments:

How do you know the explode delimiter is
<br />
instead of
<br >
or
<br>
or something else?

I'm asking both for now and for the scenario of WordPress changing things.


Jermaine Oppong comments:

I know the explode delimiter is


<br />


because the [[LINK href="http://php.net/manual/en/function.nl2br.php"]]<strong>nl2br()</strong>[[/LINK]] php function puts the <strong>br</strong> tag in that format


I have tested this already and the results are in [[LINK href="http://www.graphicbeacon.com/web-design-development/how-to-split-and-categorize-wordpress-custom-field-values/"]]my blog post[[/LINK]] when you go through it.


Clifford P comments:

You're saying <br /> is coming from nl2br(), not from how I store the list of image URLs in the custom field. I understand.


Jermaine Oppong comments:

What I am saying is that you have each image url on a seperate line. Kailey suggested this:


$images = get_post_meta( $post->ID,‘Images’, true );
$images = explode( "\n",$images );


Which does not work because php does not treat <strong>\n</strong> the way we are expecting it to, therefore not converting it the way we want - trust me I have tried this and [[LINK href="http://www.adampatterson.ca/blog/2010/09/php-handy-little-snippet-for-multiline-text-box/"]]so have others[[/LINK]] - just read the comments

The solution therefore is to convert the <strong>\n</strong> to <strong><br /></strong> using php's <strong>nl2br()</strong> and since <strong><br /></strong> is a html element, php will therefore split using this and convert the lines into an array.

If you check the earlier link to the [[LINK href="http://www.graphicbeacon.com/wp-content/uploads/2010/09/customfieldthumbs_thumb.jpg"]]image[[/LINK]], it shows that all you need to do is to put the urls on a seperate line without commas or quotes.

You mentioned that you prefer longer lines of code, but I believe that something that works with 10 lines needn't be extended to 20. The code your using simply stores bits of html into variables and echos the variables, and unless youre planning to use these more than once in multiple places for ease of change in the future, less will be more in this case. No worries if you prefer that, I guess you can expand on this and do something extensive with it in the future - glad to have made a contribution and I wish you the best with your build :-)


Clifford P comments:

Thanks for the follow-up. I have put the images on each line without commas. Also, yes, I am using the PHP/images throughout the page and ease of reading and referencing is beneficial with the longer code. I do have this webpage as a reference to your shorter code version, in case I choose to use that setup in the future. Also, the base code we've got working is this:
// images
$imagesfull = explode( "\n", get_post_meta( $postID, 'tko_images_full_screen', true));
if (is_array($imagesfull)) {

$imagesfull = array_map( 'trim', $imagesfull); //remove surrounding whitespace

//using functions defined below, create new arrays with alternate image sizes
$imagesmain = array_map( 'X3_XL', $imagesfull ); //find & replace X3 with XL in each item in array
$imagessmall = array_map( 'X3_S', $imagesfull ); //find & replace X3 with S in each item in array
$imageslargethumbnails = array_map( 'X3_Th', $imagesfull ); //find & replace X3 with Th in each item in array
$imagestinythumbnails = array_map( 'X3_Ti', $imagesfull ); //find & replace X3 with Ti in each item in array
}
}

function X3_XL( $input ) {
$input = str_replace( '-X3.jpg', '-XL.jpg', $input );
$input = str_replace( '/X3/', '/XL/', $input );
return $input;
}
function X3_S( $input ) {
$input = str_replace( '-X3.jpg', '-S.jpg', $input );
$input = str_replace( '/X3/', '/S/', $input );
return $input;
}
function X3_Th( $input ) {
$input = str_replace( '-X3.jpg', '-Th.jpg', $input );
$input = str_replace( '/X3/', '/Th/', $input );
return $input;
}
function X3_Ti( $input ) {
$input = str_replace( '-X3.jpg', '-Ti.jpg', $input );
$input = str_replace( '/X3/', '/Ti/', $input );
return $input;
}


Are you saying that <em>shouldn't</em> work? Because it's working.
If you'd like to make an enhancement to this code, please post it and add PHP comments.

The custom field contains this:

http://media.tourkick.com/Tours/20110615-4116-S-Atlanta-Ave/i-jgCV659/0/X3/4116-S-Atlanta-1-Front-001-X3.jpg
http://media.tourkick.com/Tours/20110615-4116-S-Atlanta-Ave/i-jgCV659/0/X3/4116-S-Atlanta-1-Front-001-X3.jpg
http://media.tourkick.com/Tours/20110615-4116-S-Atlanta-Ave/i-jgCV659/0/X3/4116-S-Atlanta-1-Front-001-X3.jpg
http://media.tourkick.com/Tours/20110615-4116-S-Atlanta-Ave/i-jgCV659/0/X3/4116-S-Atlanta-1-Front-001-X3.jpg
...and more...


Thanks.

2011-08-16

Utkarsh Kukreti answers:

You can add multiple values for a single "key". Try using custom_field_name as the custom field key for all your image values.

<?php
global $wp_query; $postID = $wp_query->post->ID;
foreach(get_post_meta($postID, 'custom_field_name') as $image) {
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
echo $ogimagepre.$image.$ogimagepost;
}
?>


Clifford P comments:

Although I understand and in some ways see the benefit of your idea, it's too much of a drawback to add 10, 20, 50+ custom fields. But thanks.

2011-08-16

Jurre Hanema answers:

Obviously, this isn't going to work. This way, you are creating an array with just one element which has the value 'img1','img2',...

It would be the easiest to store the meta values as a serialized array. Have you got the complete code you use to save the custom field value? In that case, I can see if I can modify everything so that it works.


Clifford P comments:

As you can tell, I've got some PHP shortcomings... How would I do serialized?

I'm not sure what "complete code" you are referring to.

I'm not sure if I'll eventually create a meta_box instead of custom_field (simply because of custom write panels, which I haven't looked too much into yet), in case that changes your suggestion.

Looking forward to your answer. Thanks.


Jurre Hanema comments:

Sorry, I hadn't even realized you had not created your own meta box yet (which was the code I referred to).

I think it would be easiest to go with Kailey Lampert's suggestion first, see if that works for you.


Clifford P comments:

I replied to Kailey. Could you describe the differences (advantages/disadvantages) of storing something like this in a meta_box instead of a custom_field?

I have no problem with one or the other, and I think I'll eventually do meta_box because of the custom write panels. Ideally, I'd like something that has fields/metas required or at least already displayed with a blank input. Since this is for a real estate listing page, I'd always want the address filled in, the photo URLs, the Realtor's name, etc.

I like custom_fields because they're don't take up much screen space, but the custom write panel setup seems to only apply to meta_boxes, not custom_fields.

And I thought they were pretty much the same thing, but maybe I'm missing a big difference.???

Thanks.