Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Register shortcode
- add_action( 'init', 'register_my_shortcode' );
- function register_my_shortcode() {
- add_shortcode( 'mycred_my_leaderboard', 'mycred_render_my_leaderboard' );
- }
- // Shortcode render only if myCRED is installed
- if ( function_exists( 'mycred_label' ) ) :
- function mycred_render_my_leaderboard( $atts, $content = NULL ) {
- $attr = shortcode_atts( array(
- 'number' => '-1',
- 'nothing' => __( 'Leaderboard is empty.', 'mycred' )
- ), $atts );
- // What user details to return besides the ID
- $attr['user_fields'] = 'display_name,user_email';
- // Query the DB
- $rankings = new myCRED_Query_Rankings( $attr );
- $rankings->get_rankings();
- ob_start(); ?>
- <table>
- <?php
- // Have results
- if ( $rankings->have_results() ) {
- // Loop though each row
- foreach ( $rankings->result as $position => $user ) {
- // Result starts with zero
- $ranking = $position+1;
- // Total
- $qty = get_user_meta( $user['ID'], 'qty', false );
- $total = array_sum( $qty );
- // Product Names
- $products = get_user_meta( $user['ID'], 'product_name', false );
- $products = implode( '<br />', $products );
- // Product IDs
- $product_ids = get_user_meta( $user['ID'], 'product_id', false );
- // Product Images
- $images = array();
- if ( ! empty( $product_ids ) ) {
- foreach ( $product_ids as $pid ) {
- $image = wp_get_attachment_image_src( get_post_thumbnail_id( $pid ), 'single-post-thumbnail' );
- if ( $image !== false )
- $images[] = '<a href="' . get_permalink( $pid ) . '"><img src="' . $image[0] . '" width="100px" height="100px" /></a>';
- }
- } ?>
- <tr class="">
- <td class="col-rank"><?php echo $ranking; ?></td>
- <td class="col-pic">
- <div class="whitebox pic-wrapper"><?php echo get_avatar( $user['user_email'], 96, home_url( '/wp-content/themes/Boke81/images/avatar.png' ) ); ?></div>
- </td>
- <td class="col-user">
- <a data-role="hovercard" data-direction="right" data-user-id="<?php echo $user['ID']; ?>" class="name"><?php echo $user['display_name']; ?></a>
- </td>
- <td class="col-owns"><?php echo $total; ?></td>
- <td class="col-img"><?php echo implode( '', $images ); ?></td>
- <td class="col-points"><?php echo mycred_format_creds( $user['cred'] ); ?></td>
- </tr>
- <?php
- }
- }
- // Else
- else { ?>
- <tr>
- <td colspan="6">No users found</td>
- </tr>
- <?php
- } ?>
- </table>
- <?php
- $output = ob_get_contents();
- ob_end_clean();
- return $output;
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement