Title: shortcode creation
Last modified: August 19, 2016

---

# shortcode creation

 *  [Igor Yavych](https://wordpress.org/support/users/flyerua/)
 * (@flyerua)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/shortcode-creation/)
 * so yoast posted one interesting thing about html sitemaps [here](http://yoast.com/html-sitemap-wordpress)
   and i decided to try. i modified it a bit to match my needs:
 *     ```
       $cats = get_categories('exclude=156');
       foreach ($cats as $cat) {
       echo '<ul></ul>';
         echo '<span style="color:#8B0000"><strong><FONT size=2>'.$cat->cat_name.'</FONT></strong></span>';
         query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
         while(have_posts()) {
           the_post();
           $category = get_the_category();
           if ($category[0]->cat_ID == $cat->cat_ID) {
             echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
       	 }
       	}
          }
       ```
   
 * it could be called ok with
    `<?php get_template_part('/partials/anime_list');?
   >`, but i want it to be called with shortcode from for example created page with
   address anime4psp.org/anime-list . How do i create shortcode? i tried like
 *     ```
       <?php
       function anime_list() {
       $cats = get_categories('exclude=156');
       foreach ($cats as $cat) {
       echo '';
       echo ''.$cat->cat_name.'';
       query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
       while(have_posts()) {
       the_post();
       $category = get_the_category();
       if ($category[0]->cat_ID == $cat->cat_ID) {
       echo ''.get_the_title().'';
       }
       }
       }
       }
   
       add_shortcode('anime-list', 'anime_list');
       ?>
       ```
   
 * and
 *     ```
       <?php
       add_shortcode('anime_list', 'anime_list');
       function anime_list()
       	{
       $cats = get_categories('exclude=156');
       foreach ($cats as $cat) {
       echo '<ul></ul>';
         echo '<span style="color:#8B0000"><strong><FONT size=2>'.$cat->cat_name.'</FONT></strong></span>';
         query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
         while(have_posts()) {
           the_post();
           $category = get_the_category();
           if ($category[0]->cat_ID == $cat->cat_ID) {
             echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
       	 }
       	}
          }
         }
   
       ?>
       ```
   
 * but none of it worked. can you help me to make it work please?

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/shortcode-creation/#post-1924907)
 * Shortcodes normally would not echo values, but instead return them.
 * If you code is actually creating the right output, but echoing it instead of 
   returning it, you can trap it with output buffering. Try it like this:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_

Viewing 1 replies (of 1 total)

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

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/shortcode-creation/#post-1924907)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
