Good day,
My limited knowledge of php has ended up creating a monstrosity of code. I am hoping someone could show me a simpler, cleaner way to accomplish this. Unfortunately I’m really really bad with php.
What I’m trying to accomplish
I am trying to code in some badgeOS icons for my WordPress site that link to corresponding lesson posts. In other words, the thumbnail from badge_post_id should link to a specific lesson_post_id. The badge thumbnail also needs to have a different look for ‘earned’ and ‘unearned’.
I have the following code that each badge thumbnail currently uses:
<!-- Badge must either be marked earned or unearned -->
<?php if (in_array($badge_post_id, $earned_achievements)) {
$css_class = 'earned-badge'; }
else {
$css_class = 'unearned-badge'; }
?>
<!-- Each badge requires this code -->
<span id="lesson-#" class="badge-class">
<a href="http://example.com/?p=$lesson_post_id" class="badge-link">
<span class="badge-icon">
<?php echo badgeos_get_achievement_post_thumbnail($badge_post_id, $image_size, $css_class) ;?>
</span>
<span class="badge-name"> LESSON TITLE </span>
</a>
</span>
I’m also using this at the start of the page:
<!-- Everything gets this -->
<?php
$image_size = array(100,100);
$earned_achievements = badgeos_get_user_earned_achievement_ids(get_current_user_id());
?>
As you can see, for 30+ badges this is a TON of code, and a huge headache to keep track of.
Ideally it would pull all the needed info from some kind of table (perhaps an array of some kind) that relates all this info:
Lesson number: $badge_post_id, $lesson_post_id, $lesson_title, $css_class
(earned or unearned)
So I can type someone much simpler to get each badge instead of adding 100 lines of code.
Something like this?
$badges could be a raw grab of a db table with columns id, lesson_id, lesson_title and whatever else you need.
@hendr1x
Your help led to me to this solution! Rather than enter the data in an array or table to be only used once, I used your code and placed it as a function.
Now I just enter the id of the lesson and badge, and the function does the rest. It seems to work well, and it certainly reduces code. Thanks so much. 🙂