Title: Creating a shortcode inside a widget
Last modified: August 21, 2016

---

# Creating a shortcode inside a widget

 *  [Mickey Kay](https://wordpress.org/support/users/mcguive7/)
 * (@mcguive7)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-shortcode-inside-a-widget/)
 * Hi there,
 * I’ve created a shortcode I’d like to use in a widget, however it’s behaving a
   bit wonky and I can’t figure it out. Here’s the code in functions.php:
 *     ```
       function related_content( $atts ) {
       	return get_the_ID();
       }
       add_shortcode('related-content', 'related_content');
       ```
   
 * The problem is that if I embed this shortcode in a page, it does indeed spit 
   out the correct ID of that page. However if I embed the code in a sidebar widget,
   it spits out a different ID entirely (it happens to be the ID of my blog page
   in this case). Any thoughts on why this is happening? Is there something about
   the context in which the shortcode is being called inside a widget? Any ideas?
 * Thanks!
    – Mickey

Viewing 1 replies (of 1 total)

 *  [ianhaycox](https://wordpress.org/support/users/ianhaycox/)
 * (@ianhaycox)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-shortcode-inside-a-widget/#post-3893900)
 * get_the_ID() is only applicable within [The_Loop](http://codex.wordpress.org/The_Loop)
   and Widgets are not displayed in the loop.
 * See [http://codex.wordpress.org/Function_Reference/get_the_ID](http://codex.wordpress.org/Function_Reference/get_the_ID)
 * What ID were you expecting or wanting in the Widget ? E.g. you may have a single
   page or post displayed or there may be a list of posts.

Viewing 1 replies (of 1 total)

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

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [ianhaycox](https://wordpress.org/support/users/ianhaycox/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-shortcode-inside-a-widget/#post-3893900)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
