[Resolved] Display author and users Badges within worpress/buddypress profile
First I would like to say that BadgeOS is a great plugin. I am looking for a solution to display individual badges on each author/users buddypress and author profile.
I am a site admin for http://SportsUnbiased.com and we could really use some knowledge on enhancing the look and feel for our users.
This isn’t tested with a wide variety of badge data, so do try it on a dev server first. It’s also in straight php code and not a shortcode or anything. If you are unfamiliar with any parts of it, especially the WP_Query part, reply and I’ll answer questions.
$current_user = wp_get_current_user(); $ids = badgeos_get_user_earned_achievement_ids( $current_user->data->ID ); $types = array(); foreach( $ids as $id ) : $types = get_post_type( $id ); endforeach; $args = array( 'post_type' => array_unique($types), 'post__in' => $ids ); $badges = new WP_Query( $args ); if( $badges->have_posts() ) : while( $badges->have_posts() ) : $badges->the_post(); ?> <?php endwhile; wp_reset_postdata(); endif;
Where would this code go, in the author.php? I might need some more guidance.
This wold go wherever you need to display the badges. Before I hand over some revised code, are you comfortable with templating and using the WordPress Loop? I want to make sure before I give you too much to handle. If you’re not, then just let me know what parts you want to display for the current user being shown, and I can revise as necessary.
Id like to think I am proficient with wordpress but I am learning new things everyday. Id say i am about 90% comfortable templating but id like as much help as i can get on this particular issue.
Id like to display up to 12 badges per author/user profile (badges resized to 75 x 75)
– possible pagination to display next page of badges
– badges listed in the top-right of the author/member profile
I really appreciate your help.. really means alot.
What sort of the details for the badges? Just the badge image and the name?
Yes, just the badge image and the name.. not sure if we need it to do anything else.
Give this a whirl, TheBigAdmin1
Pastebin’d because it got large over time.
<?php custom_badgeos_user_achievements(12,1); ?>
You get two parameters, and they should be documented in the pastebin as well. Basically pass in how many badges to fetch, and then the author ID you want. It will default to the current author page’s ID if none is passed in. It allows you show specific author/members somewhere if you want.
Let me know if you need a bit more usage help, or if it’s not working quite as it should.
In short, ignore my last reply, follow this one.
I’ve been given this as a recommended solution for you instead of what I have above. I’ve looked it over myself and can already see where it does better than what I was doing, so I’ll recommend it myself as well. Put the contents of the gist in your functions.php file.
Put the following function call wherever you want them displayed:
It takes 3 parameters but none are required. If none provided, it’ll fallback to defaults.
Author ID (default 0), Achievement Type (default all, if left empty), and limit (default 1)
No caching involved in this version as it’s not having to do any laborious querying, so that’s always a good thing. I did forget that once you have the ID, you can get most anything easily.
Micheal, the code worked flawlessly within the authors.php.. How do i tweek the code you provided to show the badge alt text when hovered as apposed to the badge name? And are the badges listed in collumns? Right now I see two badges listed under each other.
Excellent code.. We hope to thank you in more ways than one as the site continues to grow.
It looks like one would have to edit the core BadgeOS functions to get to the spot where the image tag is being generated, for which I’d have to advise against right now. However, for accessibility purposes, alt tags should be added in a future release and also made filterable for those wanting to change them. For that, I’ll be adding an issue to the GitHub repo.
If you look in the gist above, you’ll see that each badge should be wrapped in a div that has a class, as well as each badge title be wrapped in a span that has a class. Those were added to make it easy for one to hook onto via css. That said, I didn’t do anything in the way of css to alter this, so it’ll depend on your theme to style as necessary and to fit.
Sorry for the late response but I wanted to thank you again for the help. Do you have any tips on how we could improve our site. Do you know of any developers who might be interested in contributing their knowledge to help enhance the site?
I’d prefer to stay neutral on the tips part as i’m not much of a designer, and I don’t really know of any freelancers who would be able to give some time.
This appears to be resolved
I have a question for Michael Beckwith.
The code works good for me, but since I’m not very good with PHP I got a small problem. How can I change the code to display the newest earned badge? Right now I only see the very first badges the user gained.
- The topic ‘[Resolved] Display author and users Badges within worpress/buddypress profile’ is closed to new replies.