• 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.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    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.