Title: Creating shortcode
Last modified: April 25, 2020

---

# Creating shortcode

 *  [El Pablo](https://wordpress.org/support/users/el-pablo/)
 * (@el-pablo)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/)
 * I have made a custom post type called ‘modules’. Now I want to make a shortcode
   containing this modules output, but for some reason this is not working.
 * I know I have to return something, so what’s wrong with my code? When I enter
   it like this, the page get’s stuck loading ([https://prnt.sc/s5q4xq](https://prnt.sc/s5q4xq)).
 *     ```
       /**
        * Module slider
        */
       add_shortcode('module_slider', 'wbgoe_module_slider');
   
       function wbgoe_module_slider() {
           $output = function() {
               $args = array(
                   'post_type' => 'modules',
                   'posts_per_page' => 7,
                   'orderby' => 'date',
                   'order' => 'ASC'
               );
   
               $query = new WP_Query($args);
   
               while($query->have_posts()): $query->the_post();
   
               ?>
   
               <embed src="<?php the_field('url'); ?>" width="100%" height="600px"></embed>
   
               <?php
   
               endwhile;
           };
   
           return $output;
       }
       ```
   
    -  This topic was modified 6 years, 1 month ago by [El Pablo](https://wordpress.org/support/users/el-pablo/).
    -  This topic was modified 6 years, 1 month ago by [El Pablo](https://wordpress.org/support/users/el-pablo/).
    -  This topic was modified 6 years, 1 month ago by [El Pablo](https://wordpress.org/support/users/el-pablo/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcreating-shortcode%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12730206)
 * You are returning a function instead of the output to render. Remove the inner`
   function() { }`.
 *  Thread Starter [El Pablo](https://wordpress.org/support/users/el-pablo/)
 * (@el-pablo)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12732786)
 * Hi Joy,
 * indeed, this is what I had originally, but this is also not working. What is 
   wrong then with this code?
 *     ```
       add_shortcode('module_slider', 'wbgoe_module_slider');
   
       function wbgoe_module_slider() {
           $args = array(
               'post_type' => 'modules',
               'posts_per_page' => 7,
               'orderby' => 'date',
               'order' => 'ASC'
           );
   
           $query = new WP_Query($args);
   
           while($query->have_posts()): $query->the_post(); 
   
           ?>
   
           <embed src="<?php the_field('url'); ?>" width="100%" height="600px"></embed>
   
           <?php
   
           endwhile;
   
           return;
       }
       ```
   
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12733939)
 * Now you are not returning the output. It is simply output.
    Do you have the shortcode
   like `[module_slider]` in your post? Is so, do you see the `<embed>` tag before
   your `<head>` section? If not, the code is not called.
 *  Thread Starter [El Pablo](https://wordpress.org/support/users/el-pablo/)
 * (@el-pablo)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12734414)
 * Hi Joy,
 * yes, the shortcode is in my post, but nothing is rendered. The parent container
   is just empty ([https://prnt.sc/s6buar](https://prnt.sc/s6buar)). What should
   the code be like then? Because I don’t follow. How do I return the output?
    -  This reply was modified 6 years, 1 month ago by [El Pablo](https://wordpress.org/support/users/el-pablo/).
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12735036)
 * That image shows some non-HTML after the `</div>` — it has `== $0` which seems
   more like code than HTML.
 * Your function could be more like this:
 *     ```
       function wbgoe_module_slider() {
           $args = array(
               'post_type' => 'modules',
               'posts_per_page' => 7,
               'orderby' => 'date',
               'order' => 'ASC'
           );
           $query = new WP_Query($args);
           $output = '';
           while($query->have_posts()): $query->the_post(); ?>
               $output .= '
               <embed src="' . get_the_field('url') . '" width="100%" height="600px">';
           endwhile;
           wp_reset_postdata();
           return $output;
       }
       ```
   
 * Note that I removed the `</embed>` since it isn’t valid. See [https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed)
   
   Note also that you need a `type` on the embed. Note also that I changed `the_field`
   to `get_the_field`, but as this is not a WP function, I don’t know if that exists.
   Be aware that your shortcode looks like it is dependent on a plugin. Note that
   I also added `wp_reset_postdata();` so that your query data doesn’t corrupt the
   global variables.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Creating shortcode’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [Short Code](https://wordpress.org/support/topic-tag/short-code/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [Joy](https://wordpress.org/support/users/joyously/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/creating-shortcode/#post-12735036)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
