0

I'm customizing the admin edit.php screen and I have a column that displays a meta key $gear. But it only lists the first value instead of returning a list of all the values. Here's the bit that matters:

add_action( 'manage_artists_posts_custom_column', 'manage_artists_columns', 10, 2 );
function manage_artists_columns( $column, $post_id ) {
    global $post;
    switch( $column ) {
        case 'gear' :
        $gear = get_post_meta( $post_id, 'gear', true ); 
            if ( empty( $gear ) )
                echo __( '' );
            else
                printf( __( '%s' ), $gear );

            break;

        default :
            break;
    }
}

I use a similar get_post_meta() call on the front end that uses

if( $gear = get_post_meta($post->ID, 'gear') 
echo implode( $gear, ', ' );

but I couldn't get it to adapt to this application. How do i get printf to output all values?

2
  • Why are you using __() inside a printf() call here? There's nothing to be translated. Also, what do you get with var_dump( $gear )? Commented Nov 12, 2012 at 23:28
  • Short answer? I have no idea. I've cobbled together pieces of code from the interwebs to achieve something way outside of my skill set. All I know is that it works... well, this one problem aside. by var_dump( $gear ) are you referring to the single post code? $gear is a custom field I've set up so the gear each artist uses can be attached to their profile. Commented Nov 12, 2012 at 23:36

1 Answer 1

0
$gear = get_post_meta( $post_id, 'gear', true );

… will return the last value only, because you told it do that with the last parameter. Drop it, and you will get an array of all values:

$gear = get_post_meta( $post_id, 'gear' );

Then print it with:

print join( $gear, ', ' );

And as Chip has pointed out: Do not use the translation function for values that are not translatable. And even translatable values need a text domain.

3
  • JOIN!! Thank you so much. I tried so many variations of print, echo, implode, etc, just throwing spagetti at the wall. join is new to me and that did the trick. Only for anyone else this will help, the "true" needs to be removed :) (I assume that was a typo). Commented Nov 12, 2012 at 23:42
  • Yes, it was a typo. :) join() is an alias of implode().
    – fuxia
    Commented Nov 12, 2012 at 23:43
  • I'm learning :) That's the main reason I am doing this by hand instead of searching for a plethora of plug-ins. Thanks again! Commented Nov 12, 2012 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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