0

I created a shortcode with attributes that modifies the query with the following code:

function LoopShortcodeAtts($atts) { ?>

        <?php get_template_part( 'shortcode/shrt_loop_con_atts' ) ?>


                <?php   }


          add_shortcode( 'racconti', 'LoopShortcodeAtts' );

in shrt_loop_con_atts.php i entered the specific code:

<?php $atts = shortcode_atts( array(
    'post_type' => '',
    'n_posts' => '4',
    'orderby' => '',

), $atts, 'racconti' )
 ?>


<div class="container">
            <div class="row">
              <?php $pagina_attuale = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
              <?php

              $args = array(
                   'post_type' => $atts["post_type"],
                   'posts_per_page' => $atts['n_posts'],
                   'orderby' => $atts['orderby'],
                  'paged' => $pagina_attuale,
              );

              $eventi = new WP_Query($args); ?>


        <?php if ($eventi->have_posts()) :  while ($eventi->have_posts()) : $eventi->the_post(); ?>



                       <div class="col-md-4 col-sm-4">
                           <?php var_dump($atts) ?>


                          and other HTML CODE

          <?php endwhile; ?>
           </div> <!--.row articolo -->


           <?php// echo paginate_links(array(
            // 'total' => $eventi->max_num_pages


          // )); ?>

          <?php else : ?>

            <?php echo "Racconti non trovati";?>

         <?php endif; ?>

if I set the parameters directly with the attributes for example the 4 for posts_per_page works but if I recall the shortcode in the editor with :

[racconti post_type ="learncare_courses"]

which is the name of my CPT, the attr not working and the var_dump give me this:

array(3) { ["post_type"]=> string(0) "" ["n_posts"]=> string(1) "4" ["orderby"]=> string(0) "" }

I can not understand why I can not set the attributes from the editor. Where am I wrong?

  • I think the point is that the $atts are not being passed properly to the shortcodes – Mario gasparella Dec 16 '17 at 19:42
  • only query vars are passed on get_template_part. in your case, you can store the attributes in a global variable. – mmm Dec 16 '17 at 20:25
  • thanks . i resolved with include(locate_template('dir')) – Mario gasparella Dec 16 '17 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.