Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using this code here to grab all my custom fields from a post for use in an gallery array.

http://www.kevinleary.net/get-all-custom-fields-attached-post-page-post-type/

function:

//  Get all custom fields attached to a page
if ( !function_exists('base_get_all_custom_fields') ) {
function base_get_all_custom_fields()
{
    global $post;
    $custom_fields = get_post_custom($post->ID);
    $hidden_field = '_';
    foreach( $custom_fields as $key => $value ){
        if( !empty($value) ) {
            $pos = strpos($key, $hidden_field);
            if( $pos !== false && $pos == 0 ) {
                unset($custom_fields[$key]);
            }
        }
    }
    return $custom_fields;
}
}

single.php:

// Get all custom fields attached to this post and store them in an array
$custom_fields = base_get_all_custom_fields();
if( !empty($custom_fields) ) {
print_r($custom_fields);
}

It works, but outputs text like this around the custom post Array ( [photo1] => Array ( [0] =>

Is there a way to customise what you wrap before & after a custom field output, and remove the array text?

What I'm trying to achieve is

{image : 'http://website.com/slides/photo1.jpg'}, 
{image : 'http://website.com/slides/photo2.jpg'},  
{image : 'http://website.com/slides/photo3.jpg'}, 

so I need to wrap {image : ' before and '}, after the link.

I can sort of do it with single queries but the array system would be better

{image : '<?php echo $custom_fields['photo1'][0]; ?>'},
{image : '<?php echo $custom_fields['photo2'][0]; ?>'},
{image : '<?php echo $custom_fields['photo3'][0]; ?>'},
share|improve this question
add comment

2 Answers

if( !empty($custom_fields) ) {
    $output = '';
    foreach( $custom_fields as $key => $value ){
        foreach( $value as $val ){
            $output .= "{".$key." : '".$val."'},";
        }
    }
    echo $output;
}
share|improve this answer
    
Beat me by seconds... :) –  kaiser Jun 18 '11 at 14:25
    
hi - that doesn't seem to work? just getting a blank page. Do i need to amend something? –  david arthur Jun 18 '11 at 15:48
    
Should this be added to my existing code or replace it? –  david arthur Jun 18 '11 at 17:21
    
hi - could you let me know where that code goes -is it the functions or tempalte file? –  david arthur Jun 18 '11 at 20:37
    
this would replace the if( !empty($custom_fields) ) { } section in your single.php file –  Milo Jun 18 '11 at 21:01
show 1 more comment

I'm not really sure about what you're asking, but as far as i understood you're trying to get some "stuff" before/after every custom field value?

EDIT: Has to be placed in your functions.php file

function wpse20348_custom_field_output( $echo = false )
{
    $custom_fields = base_get_all_custom_fields();

    if ( empty( $custom_fields ) ) 
        return;

    $output = '';

    $prefix = '{image : ';
    $suffix = '}';

    foreach ( $custom_fields as $key => $field )
    {
        $output .= $prefix.$field.$suffix;
    }

    if ( $echo === false )
        return $output;

    return print $output;
}
// Call it like this in your theme:
wpse20348_custom_field_output( true );

// or save it for further processing:
$wpse_custom_fields = wpse20348_custom_field_output();

It might be that you need to change $prefix to your needs. It might also be that you need to append [0] to $field inside the foreach loop.

share|improve this answer
    
do I wrap this in php tags in template? just gives me errors.<?php wpse20348_custom_field_output( true ); ?> –  david arthur Jun 18 '11 at 15:48
    
Yes, you need to wrap everything that's not html in php tags. Plus: See update. –  kaiser Jun 18 '11 at 16:07
    
Btw: print_r( $var ); and var_dump( $var ); are just there to show you what's inside a variable, array or object. They are meant for debugging, not production. To output something use print or echo and read a little about php basics on the official resource. –  kaiser Jun 18 '11 at 16:13
    
Hmm I have this in functions function wpse20348_custom_field_output( $echo = false ) {     $custom_fields = base_get_all_custom_fields();     if ( empty( $custom_fields ) )         return;     $output = '';     $prefix = '{image : ';     $suffix = '}';     foreach ( $custom_fields as $key => $field )     {         $output .= $prefix.$field.$suffix;     }     if ( $echo === false )         return $output;     return print $output; } }' and this <?php wpse20348_custom_field_output( true ); ?>` in single.php but I'm just getting blank. –  david arthur Jun 18 '11 at 16:25
1  
If you got a blank screen, I'd guess you forgot to open or close html tags in the right place. That's what I meant. You can edit your Q with a link to a pastebin to your functions.php and anotherone with your single.php code. But here the journey ends for me. Learn some basic php and html. Else you'll loose much more time than you'd invest into learning the stuff (approximately 2 hours). So (for the last time): PLEASE READ THE MANUAL! –  kaiser Jun 18 '11 at 19:00
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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