• Resolved Daniel Lemes

    (@daniel_iceman)


    Any idea why my badges are not showing images?
    I’m using the snippet:

    // Make sure our website does not crash if myCRED is disabled.
    if ( function_exists( 'mycred_get_users_badges' ) ) :
    	// Only needed if you do not have the author object. We want an ID
    	$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
    	$author_id = $author->ID;
    
    	// Get the authors earned badges
    	$authors_badges = mycred_get_users_badges( $author_id );
    	// If the author has badges, loop though them and show each image.
    	if ( ! empty( $authors_badges ) ) {
    		foreach ( $authors_badges as $badge_id )
    			echo '<img src="' . get_post_meta( $badge_id, 'main_image', true ) . '"' . $width . $height . ' class="mycred-badge earned" alt="' . get_the_title( $badge_id ) . '" title="' . get_the_title( $badge_id ) . '" />';
    	}
    endif;

    And the HTML in response is:

    <img src="" class="mycred-badge earned" alt="" title="">

    …even after clear caches.

    https://wordpress.org/plugins/mycred/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author myCred

    (@designbymerovingi)

    If you are using 1.6, then the mycred_get_users_badges function has been changed with the introduction of “Levels”.

    The difference is that instead of just returning an array of badge IDs, the array is now associated and holds the badge ID and the level.

    Change:

    foreach ( $authors_badges as $badge_id )

    to:

    foreach ( $authors_badges as $badge_id => $level )

    Alternativly if you want to show the appropriate level image then your loop could look like this:

    foreach ( $users_badges as $badge_id => $level ) {
    
    	$level_image = get_post_meta( $badge_id, 'level_image' . $level, true );
    	if ( $level_image == '' )
    		$level_image = get_post_meta( $badge_id, 'main_image', true );
    
    	$title = get_the_title( $badge_id );
    
    	echo '<img src="' . $level_image . '" class="mycred-badge earned badge-id-' . $badge_id . ' level-' . $level . '" alt="' . $title . '" title="' . $title . '" />';
    }

    Thread Starter Daniel Lemes

    (@daniel_iceman)

    Perfect, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Not showing badge image’ is closed to new replies.