Title: Insert Shortcode Into Theme Template
Last modified: August 22, 2016

---

# Insert Shortcode Into Theme Template

 *  [mobetman2](https://wordpress.org/support/users/mobetman2/)
 * (@mobetman2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/)
 * Can and how do we insert the code into a theme template? I tried the do_shortcode
   function but it didn’t work. Any suggestions?
 * [https://wordpress.org/plugins/tagregator/](https://wordpress.org/plugins/tagregator/)

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

 *  Plugin Contributor [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569541)
 * The following works for me:
    `<?php echo do_shortcode( '[tagregator hashtag="
   wordpress"]' ); ?>`
 * Did you remember to `echo` the string that `do_shortcode()` returns?
 *  Thread Starter [mobetman2](https://wordpress.org/support/users/mobetman2/)
 * (@mobetman2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569554)
 * Ian,
 * Thanks for getting back so quickly. I figured out the issue with the shortcode
   but now I’m having another related challenge I’m hoping you can offer advice 
   on.
 * I’m setting up an event website and want to use your plugin to track social buzz
   around each event hashtag. I’m using your shortcode in a sidebar widget which
   works great.
 * Here’s the challenge. Each event will have a different hashtag so I can’t use
   a static string. My solution was to setup a custom field and use PHP to grab 
   the hashtag per event post using the following code:
 *     ```
       <?php
              $postid = get_the_ID();
              $posthashtag = get_post_field( 'hashtag', $postid );
         ?>
       ```
   
 * The code works and it retrieves the hashtag perfectly. I have it output to show
   a page visitor what hashtag is being searched.
 * However, when I use the code inside of your plugin shortcode it doesn’t work.
 * [tagregator hashtag= “<?php echo get_post_field( ‘hashtag’, $postid ) ?>”]
 * The code is actually working but the plugin seems to executing before the retrieving
   the hashtag. I think this is happening because when I tried the following code:
 *     ```
       [tagregator hashtag= "#wordcamp, <?php echo get_post_field( 'hashtag', $postid ) ?>"]   or 
   
       [tagregator hashtag= "#wordcamp, <?php echo $posthashtag ?>"]
       ```
   
 * Only the wordcamp hashtag displays but if instead of using the code and I use
   two strings like #wordcamp, #nba they both work.
 * I tried executing the code in the child theme functions.php file, page template(
   which is how I started this thread) but nothing seems to be working.
 * I then tried to use the above code and store the entire shortcode into a variable(
   $posthash) and then echo that variable thinking it might return the shortcode
   without the embedded code.
 * It didn’t work because it somehow conflicted with the plugin. However, if I changed
   the word “tagregator” to anything else like “regator” the code worked and display
   the entire shortcode in the sidebar as text. But if the correct shortcode was
   used, it returned blank.
 * It’s been frustrating for me to try and devise a fix for this. Any advice on 
   how to insert a dynamic variable into the shortcode?
 *  Thread Starter [mobetman2](https://wordpress.org/support/users/mobetman2/)
 * (@mobetman2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569785)
 * Any suggestions on how to fix the issue I mentioned in my earlier post? Would
   like to get this working if possible.
 *  Thread Starter [mobetman2](https://wordpress.org/support/users/mobetman2/)
 * (@mobetman2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569795)
 * Anyone have a suggestion on how to get the dynamic variable to execute BEFORE
   the tagregator shortcode? I’ve researched and tried everything I know but no 
   success.
 *  [Dadragon](https://wordpress.org/support/users/dadragon/)
 * (@dadragon)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569836)
 * Did you get this to work? I wanted to do something similar so wrote this code(
   I am new to php so if anyone wants to clean it up feel free):
 *     ```
       <?php
       $posttags = get_the_tags();
       $phtag = '';
       $count=0;
       if ($posttags) {
         foreach($posttags as $tag) {
           $count++;
           if (1 == $count) {
       	  $phtag = $tag->name ;
       	  trim($phtag, " ");
           }
         }
       }
       $t='';
       $t .= '[tagregator hashtag="#'.$phtag.'" layout="one-column"]';
   
       if ($t ==='[tagregator hashtag="#" layout="one-column"]') {
       	echo "";
       	} else {
       	echo do_shortcode( $t );
       	}
   
       ?>
       ```
   
 *  Thread Starter [mobetman2](https://wordpress.org/support/users/mobetman2/)
 * (@mobetman2)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569837)
 * [@dadragon](https://wordpress.org/support/users/dadragon/), no I never got it
   to work properly. Is the code you wrote working for you?
 *  [Dadragon](https://wordpress.org/support/users/dadragon/)
 * (@dadragon)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569838)
 * yes it works fine, the first part checks if there are multiple tags and then 
   pulls the first tag into $phtag
 * You might want to edit the layout= bit as per the FAQ

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

The topic ‘Insert Shortcode Into Theme Template’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/tagregator_99d9ed.svg)
 * [Tagregator](https://wordpress.org/plugins/tagregator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/tagregator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/tagregator/)
 * [Active Topics](https://wordpress.org/support/plugin/tagregator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/tagregator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/tagregator/reviews/)

 * 7 replies
 * 3 participants
 * Last reply from: [Dadragon](https://wordpress.org/support/users/dadragon/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/insert-shortcode-into-theme-template/#post-5569838)
 * Status: not resolved