Skip to content

Instantly share code, notes, and snippets.

@brichards
Last active January 12, 2024 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brichards/efca231489941b5c0a63 to your computer and use it in GitHub Desktop.
Save brichards/efca231489941b5c0a63 to your computer and use it in GitHub Desktop.
Earned badges function
<?php
/**
* Display earned badges for a given author
*
* @param integer $author_id The user's ID, defaults to current post author
* @param string $achievement_type A specific type of achievement to list (e.g. 'badge'), default: all
* @param integer $limit Number of achievements to include, default: 10
*/
function custom_badgeos_get_author_achievements( $author_id = 0, $achievement_type = '', $limit = 10 ) {
// Get the post author if no specific user specified.
if ( ! $author_id ) {
global $post;
$author_id = $post->post_author;
}
// Grab the user's current achievements, without duplicates
$achievements = array_unique( badgeos_get_user_earned_achievement_ids( $author_id, $achievement_type ) );
// Setup a counter
$count = 0;
// Loop through the achievements
if ( ! empty( $achievements ) ) {
foreach( $achievements as $achievement_id ) {
// If we've hit our limit, quit
if ( $count >= $limit )
break;
// Output our achievement image and title
echo badgeos_get_achievement_post_thumbnail( $achievement_id );
echo '<span class="badgeos-title-wrap">' . get_the_title( $achievement_id ) . '</span>';
// Increase our counter
$count++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment