SHOW:
|
|
- or go back to the newest paste.
1 | /** | |
2 | * Display currently earned badges for author. | |
3 | * @param integer $count How many posts to display per page | |
4 | * @param integer $author_id ID of the author to display. Defaults to ID 1 | |
5 | * @return mixed echo'd badge loop. | |
6 | */ | |
7 | - | function custom_badgeos_user_achievements( $author_id = 1, $count = 1 ) { |
7 | + | function custom_badgeos_user_achievements( $count = 1, $author_id = 1 ) { |
8 | - | //Use the current author page's if no specific user specified. |
8 | + | //Use the current user if no specific user specified. |
9 | if ( 1 === absint( $author_id ) ) { | |
10 | global $post; | |
11 | $author_id = $post->post_author; | |
12 | } | |
13 | ||
14 | //Re-fetch our data if the transient has expired. | |
15 | if ( false === ( $badges = get_transient( 'custom_badgeos_user_achievements' ) ) ) { | |
16 | //Grab the user's current list of achievements, by ID | |
17 | $ids = badgeos_get_user_earned_achievement_ids( $author_id ); | |
18 | ||
19 | $types = array(); | |
20 | foreach( $ids as $id ) : | |
21 | //shuffle the badge type into its own array. | |
22 | $types[] = get_post_type( $id ); | |
23 | endforeach; | |
24 | //Assign our arguments based on passed in parameters and unique badge types and only earned badges by ID. | |
25 | $args = array( | |
26 | 'posts_per_page' => $count, | |
27 | 'post_type' => array_unique($types), | |
28 | 'post__in' => $ids | |
29 | ); | |
30 | $badges = new WP_Query( $args ); | |
31 | //store our resulting WP_Query object in a transient for one hour. | |
32 | set_transient( 'custom_badgeos_user_achievements', $badges, 60*60 ); | |
33 | } | |
34 | //Loop through our badges as we would any other post listing, display the parts we want. | |
35 | if( $badges->have_posts() ) : while( $badges->have_posts() ) : $badges->the_post(); ?> | |
36 | <?php echo badgeos_get_achievement_post_thumbnail(); ?> | |
37 | <span class="badgeos-title-wrap"><?php the_title(); ?></span> | |
38 | <?php endwhile; wp_reset_postdata(); endif; | |
39 | } |