0

I have a problem with custom fields that I have been trying to figure out for a while, but with no luck.

I have a section on the site I'm building that let's the users publish content form the the front end. The users content is stored in several custom fields attached to the post. So far so good, everything works as expected.

The problem I have is to figure out the best way to fetch this content.

The post that the user is publishing is divided in small chunks, they can for example add an image, a text, or a video.

The custom fields are created like this for every piece of information:

custom_text
custom_text1
custom_text2

custom_image
custom_image1
custom_image2

custom_video
custom_video1
custom_video2

... and so on.

The problem is, I can never be sure what the user decides to add to his post, and the custom fields for every post will look something like this:

custom_image
custom_text1
custom_image2
custom_video3
custom_text4

It has to be saved this way, for the sorting to work. The content needs to be displayed in the order that the user chose when creating the post.

So, what I need is an advanced custom field loop that loops through these custom fields, and display the content accordingly.

Something like:

php start loop
counter = 0

if custom_text (display custom_text)
else if custom_image (display custom_image)
else if custom_video (display custom_video)

counter++;
end loop

I'm thinking that the counter could increase with one every time, and then loop through it all again for number 1, 2 3 etc.

We don't know how many fields will be created, but there will be a maximum of 40 chunks, so the last digit will be 39.

I have experimented a lot, but haven't yet been able to find the solution.

Maybe someone could point me in the right direction?

Thanks!

// Jens.

0

You need a loop within a loop - loop through a counter, and for each value, check for the existence of each key with the current counter value appended.

// max custom field index
$number = 40;
// the counter
$counter = '';
// the meta keys to check for
$keys = array(
    'custom_text',
    'custom_image',
    'custom_video'
);
// all our custom field values
$custom_fields = get_post_custom( get_the_ID() );

// loop over our counter
while( $counter < $number ){
    // loop over each of the keys
    foreach( $keys as $key ){
        // check if a custom field with key + counter exists
        if( isset( $custom_fields[ $key . $counter ] ) ){
            // output the field
            // values will be in an array, 0 is the first index.
            // you can loop over these as well if you have multiple values.
            echo $custom_fields[ $key . $counter ][0];
        }
    }
    // increment the counter
    $counter++;
}
  • Wow, thanks a lot! That's awesome, and just what I need! I have one question: If I need some more control over styling and want to add more fields, say something like this: Headline Image/Video Text block A link How can I check what kind of field it is, and display it's styling accordingly? You're writing: "You can loop over these as well, if you have multiple values", but I'm not sure if that´s what I need to do? P.S. Sorry for the late reply, some things came up in "real life" :) @Milo – jenseo Nov 27 '14 at 18:07
  • Hey @Milo , I have found a way to style the content, so this is solved. I have one other question though: I have one field that starts with the number 1, instead of empty, and then continues with 2 when the other fields is on number 1. At the moment I can't change this behaviour, so I wondered if there is a way to make an if statement that only applies to this field. Something like: $thespecific_key . $counter + 1 Any ideas for this scenario? – jenseo Nov 29 '14 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.