• Resolved scep2

    (@scep2)


    I’m trying to get thumbnails of certain badges in a content-template. This works with the following code (the essential function is badgeos_get_achievement_post_thumbnail):

    $posts = get_field('badges');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php echo badgeos_get_achievement_post_thumbnail( $post->ID ); ?>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endforeach; ?>
    <?php endif; ?>

    However, what I wanted is to show earned badges like they are displayed now (coloured), but to show unearned badges greyed out. How can I achieve this?

    https://www.ads-software.com/plugins/badgeos/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hi scep2, thank you for moving this here.

    The main thing for this is going to be the need to get the current user, and check if they have earned each badge being iterated over in your foreach loop.

    I recommend using something like the following before the loop starts:

    $earned_achievements = badgeos_get_user_earned_achievement_ids( get_current_user_id() );

    That way you have an array of what the user has earned, if anything. If none yet, it’ll return an empty array.

    With that $earned_achievements variable array, you could use something like PHP’s in_array() and use the $post->ID from your loop, which will correspond to the achievement ID you’re checking for.

    If the post ID is in the $earned_achievements array, they’ve earned that achievement, else they haven’t, and you can do something different with it.

    Hopefully that all makes sense, but please let me know if you have any questions.

    https://badgeos.org/api/source-function-badgeos_get_user_earned_achievement_ids.html#339-363

    Thread Starter scep2

    (@scep2)

    Hi Michael

    Thank you very much. This works like a charm. If someone has the same need, here is the complete code.

    CSS

    span.unearned_achievement {
    	opacity: .4;
    }

    PHP (WordPress-Template)

    <?php
    $posts = get_field('badges'); // This is a custom field generated with the Advanced Custom Fields plugin
    
    if( $posts ): ?>
    	<?php $earned_achievements = badgeos_get_user_earned_achievement_ids( get_current_user_id() ); ?>
    	<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    		<?php setup_postdata($post); ?>
    		<a href="<?php echo get_permalink( $post->ID ) ?>" target="_new">
    			<?php
    			if (in_array($post->ID, $earned_achievements)) {
    				echo badgeos_get_achievement_post_thumbnail( $post->ID );
    			} else {
    				echo "<span class='unearned_achievement'>"; // This allows for changing opacity with CSS
    				echo badgeos_get_achievement_post_thumbnail( $post->ID );
    				echo "</span>";
    			}
    			?>
    		</a>
    		<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    	<?php endforeach; ?>
    <?php endif; ?>
    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like a workable solution. ??

    In case anyone is curious about an alternative, say if you want to group them by earned/unearned status, I’d go about things this following way.

    In the else portion, which is the unearned side, stash the achievement in a new array instead of display anything. After that, do a foreach loop on that new array after the first loop is complete. It’d separate out the earned statuses and display one group after the other.

    mkocher

    (@mkocher)

    Is there a way to do this without using a custom field? I’ve been trying to achieve something like this forever! Sadly, I only know a small amount of php and the like.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    You’d have to ask scep2 what the scenario is for needing to store some of the information in Advanced Custom Fields.

    This is EXACTLY what I need to do, but i would like to do it in a widget. Can it be done this way? I’m not that strong with template files and Advanced Custom Fields Pro (though I have the plugin), so any guidance is super appreciated.

    Are there simpler ways to accomplish this if the content is going to be in a widget instead of in a template file?

    thanks

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    It’s going to be at least a little bit of custom coding to achieve, regardless. If you can make the output from scep2’s work above into a shortcode, you could drop the shortcode into a text widget. May need to make sure that text widgets can parse shortcodes.

    https://digwp.com/2010/03/shortcodes-in-widgets/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get achievement post thumbnail with earned/unearned-distinction’ is closed to new replies.