Zaeo Zaeo - 4 months ago 9
PHP Question

Print Gravatar using sprintf

I'm trying to get the gravatar for wordpress to work in a sprintf statement.

<?php
if ( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories || 'on' === $show_comments ) {
printf( '<div class="post-wrapper"><p class="post-meta">%1$s %2$s %3$s %4$s %5$s <span class="comments">%6$s</span></p></div>',
(
'on' === $show_author
? et_get_safe_localization( sprintf( __( 'By: %s', 'et_builder' ), '<span class="vcard2">' . get_avatar( get_the_author_meta('ID'), 80) . '</span><span class="author">' . et_pb_get_the_author_posts_link() . '</span>' ) )
: ''
),
(
( 'on' === $show_author && 'on' === $show_date )
? ' '
: ''
),
(
'on' === $show_date
? et_get_safe_localization( sprintf( __( '%s', 'et_builder' ), '<span class="published">' . esc_html( get_the_date( $meta_date ) ) . '</span>' ) )
: ''
),
(
(( 'on' === $show_author || 'on' === $show_date ) && 'on' === $show_categories)
? ' '
: ''
),
(
(( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories ) && 'on' === $show_comments)
? ' | '
: ' '
),
(
'on' === $show_comments
? sprintf( esc_html( _nx( '1 Comment', '%s Comments', get_comments_number(), 'number of comments', 'et_builder' ) ), number_format_i18n( get_comments_number() ) )
: ''
)
);
}

?>


The problem line lies with the sprintf function.

? et_get_safe_localization( sprintf( __( 'By: %s', 'et_builder' ), '<span class="vcard2">' . get_avatar( get_the_author_meta('ID'), 80) . '</span><span class="author">' . et_pb_get_the_author_posts_link() . '</span>' ) )


If i put echo in front, syntax error. If i close it with a semi-colon, syntax error.

It doesn't seem to be acknowledging the
get_avatar( get_the_author_meta('ID'), 80)
at all, it just gets ignored. Nothing is output to the html.

Show avatars is checked in the WordPress discussion options.

The test page where this is going to be output: Test page

Answer

After many hours of frustration, I finally figured it out.

<?php
                if ( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories || 'on' === $show_comments ) {
                    echo get_avatar( $post->post_author, 80 );
                    printf( '<div class="post-wrapper"><p class="post-meta">%1$s %2$s %3$s %4$s %5$s <span class="comments">%6$s</span></p></div>',
                        (
                            'on' === $show_author
                                ? et_get_safe_localization( sprintf( __( 'By: %s', 'et_builder' ), '<span class="vcard2">' . get_avatar( $post->post_author, 80 ) . '</span><span class="author">' .  et_pb_get_the_author_posts_link() . '</span>' ) )
                                : ' '
                        ),
                        (
                            ( 'on' === $show_author && 'on' === $show_date )
                                ? '  '
                                : ''
                        ),
                        (
                            'on' === $show_date
                                ? et_get_safe_localization( sprintf( __( '%s', 'et_builder' ), '<span class="published">' . esc_html( get_the_date( $meta_date ) ) . '</span>' ) )
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date ) && 'on' === $show_categories)
                                ? '  '
                                : ''
                        ),
                        (
                            (( 'on' === $show_author || 'on' === $show_date || 'on' === $show_categories ) && 'on' === $show_comments)
                                ? ' | '
                                : ' '
                        ),
                        (
                            'on' === $show_comments
                                ? sprintf( esc_html( _nx( '1 Comment', '%s Comments', get_comments_number(), 'number of comments', 'et_builder' ) ), number_format_i18n( get_comments_number() ) )
                                : ''
                        )
                    );
                } 

?>

I had to include the echo before the sprintf statement and deal with it via css.

Comments