• Resolved luiscarvalho

    (@luiscarvalho)


    Hello everyone.

    I created a custom shortcode for my Vikinger theme that displays achievements with a filter for the type of achievement. I did it this way so that it displays using the existing theme template. However, I need it to display only the achievements that the user has and not all the achievements on the site. How can I do this?

    Code:

    /*
    * Shortcode para Menu Mochila
    */
    
    function my_achievements_shortcode( $atts ) {
      
    // Recebe os atributos do shortcode
      $atts = shortcode_atts( array(
        'type' => 'mascotes',
        'id' => ''
      ), $atts );
    
      // Define a variável $achievement_type
      $achievement_type = $atts['type'];	
    
      // Exibe as conquistas
      $all_achievements = vikinger_gamipress_get_logged_user_achievements($achievement_type);
    
      // Define o número de itens por página
      $items_per_page = 12;
    
      // Obtém o número total de conquistas
      $total_items = count($all_achievements);
    
      // Calcula o número total de páginas
      $total_pages = ceil($total_items / $items_per_page);
    
      // Obtém o número da página atual
      $paged = isset($_GET[$atts['id']]) ? intval($_GET[$atts['id']]) : 1;
    
      // Define o início e o fim dos itens a serem exibidos na página atual
      $start = ($paged - 1) * $items_per_page;
      $end = $start + $items_per_page;
    
      // Define o link da paginação
      $page_link_base = add_query_arg($atts['id'], '', get_permalink());
    
      // Define os argumentos para a exibição das conquistas na página atual
      $achievement_args = [
    	'user_points' => vikinger_gamipress_get_logged_user_points(),
        'achievement_complete_info' => false,
      ];
    
      if ($achievement_type === 'quest') {
        $achievement_args['achievement_image_wrap'] = true;
      }
    
      // Definir o número máximo de páginas a serem exibidas na paginação
    $max_pages = 5;
    
    // Exibe as conquistas da página atual
    ob_start(); // inicia o buffer de saída
    ?>
    <?php if (empty($all_achievements)) : ?>
        <p id="vazio">🪰 Tudo vazio por aqui, volte mais tarde.</p>
    <?php else : ?>
    <div class="grid grid-3-3-3-3 medium-space centered-on-mobile grid-stretched grid-items uniquegrid">
      <?php foreach (array_slice($all_achievements, $start, $items_per_page) as $achievement) :
          $achievement_args['achievement'] = $achievement;
          get_template_part('template-part/achievement/achievement', 'item-box', $achievement_args);
      endforeach; ?>
    </div>
    <?php endif; ?>
    <?php if ($total_pages > 1) : ?>
    <div class="pagination">
      <ul>
    
        <?php if ($paged > 1) { ?>
        <li>
          <a href="<?php echo add_query_arg($atts['id'], ($paged - 1), $page_link_base); ?>" class="prev">&laquo;</a>
        </li>
        <?php } ?>
    
        <?php
        $start_page = max(1, $paged - floor($max_pages / 2));
        $end_page = min($total_pages, $start_page + $max_pages - 1);
    
        for ($i = $start_page; $i <= $end_page; $i++) :
          // Adiciona o número da página à URL base
          $page_link = add_query_arg($atts['id'], $i, $page_link_base);
          ?>
          <li>
            <?php if ($paged === $i) : ?>
            <span class="current active"><?php echo $i; ?></span>
            <?php else : ?>
            <a href="<?php echo esc_url($page_link); ?>"><?php echo $i; ?></a>
            <?php endif; ?>
          </li>
        <?php endfor; ?>
    
        <?php if ($paged < $total_pages) { ?>
        <li>
          <a href="<?php echo add_query_arg($atts['id'], ($paged + 1), $page_link_base); ?>" class="next">&raquo;</a>
        </li>
        <?php } ?>
    
      </ul>
    </div>
    <?php endif; ?>
    
      <?php
      return ob_get_clean(); // retorna o conteúdo do buffer de saída e limpa ele
    }
    
    add_shortcode( 'my_achievements', 'my_achievements_shortcode' );
Viewing 1 replies (of 1 total)
  • Plugin Author Ruben Garcia

    (@rubengc)

    Hi @luiscarvalho

    I am so sorry but we do not offer support for customizations like that since our main focus is the development and maintenance of GamiPress, AutomatorWP and all official add-ons.

    Anyway, any WordPress custom development agency or freelancer can handle it for you.

    If you do not know one, we recommend checking our list of trusted experts that can estimate any type of job for you here: https://gamipress.com/customizations/

Viewing 1 replies (of 1 total)

The topic ‘How to filter my shortcode?’ is closed to new replies.