Title: PHP Shortcode
Last modified: June 21, 2022

---

# PHP Shortcode

 *  [nicoleat](https://wordpress.org/support/users/nicoleat/)
 * (@nicoleat)
 * [3 years, 12 months ago](https://wordpress.org/support/topic/php-shortcode-27/)
 * I need to create a custom PHP shortcode that I can insert into the main template
   that will look at the post ID, see to which tag it is assigned, grab that tag
   ID, and based on that ID, display the custom fields values (description, asx 
   code, website url, asx url, logo) for the tag.
 * This way the post will show (where the shortcode has been inserted) some specific
   information depending on the tag that has been assigned to it.
    -  This topic was modified 3 years, 12 months ago by [nicoleat](https://wordpress.org/support/users/nicoleat/).

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 12 months ago](https://wordpress.org/support/topic/php-shortcode-27/#post-15747416)
 * Get the current post from global $post. This will only work in a standard WP 
   loop. If post content is output in some other context, this will not work unless
   the post ID is passed to the shortcode handler via shortcode attributes. If you
   rely upon global $post, your shortcode should have an alternative process for
   when it’s not within a WP loop context.
 * Get the assigned tags with wp_get_post_tags(). Tags are always returned in an
   array, even if there’s only one tag. How would your shortcode know which tag 
   to use if there’s more than one? It may suffice to arbitrarily use the first 
   tag in the array.
 * Assuming custom fields are saved in post meta, get field values with get_post_meta().
 * Be sure all shortcode output is returned from the handler function. Never echo
   out content from a shortcode handler.

Viewing 1 replies (of 1 total)

The topic ‘PHP Shortcode’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [tags](https://wordpress.org/support/topic-tag/tags/)

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

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
