Title: Shortcode / function only working for first post rendered by loop archive page
Last modified: August 31, 2016

---

# Shortcode / function only working for first post rendered by loop archive page

 *  Resolved [andygo12](https://wordpress.org/support/users/andygo12/)
 * (@andygo12)
 * [10 years ago](https://wordpress.org/support/topic/shortcode-function-only-working-for-first-post-rendered-by-loop-archive-page/)
 * Hi there,
 * I have a custom post archive displaying post and author meta at roadtripsharing.
   com/road_trip
 * In the archive template I added a do_shortcode(); in the loop, specifically related
   to the “New Message” button which opens a dialogue to send a direct message to
   users. Clicking the button only triggers the dialogue for the first post display(
   though the button does show on all posts, which is even more bizarre). I tried
   this with a gravity forms shortcode as well and it only rendered the form in 
   the first (top-most) post on the page.
 * To simplify the question and perhaps make it more useful to others: Suppose I
   only wanted to show the post title and the shortcode. Would/should this be correct
   to make the shortcode fully work for all posts?
 *     ```
       <?php get_header(); ?>
           <?php $road_trips = new WP_Query(array(
           'post_type' => 'road_trip'
           )); ?>
           <?php while($road_trips->have_posts()) : $road_trips->the_post(); ?>
           <div>
             <?php echo get_the_title(); ?>
             <?php echo do_shortcode('[shortcode]'); ?>
           </div>
           <?php endwhile; ?>
           <?php get_footer(); ?>
       ```
   
 * Here is the code for the actual shortcode as far as I can tell from the plugin
   source code:
 * [Large code excerpt removed by moderator per [forum rules](https://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use [the pastebin](http://wordpress.pastebin.com/) for all large code 
   excerpts. It works better anyway.]
 * The add_shortcode call, also in plugin file:
 *  `add_shortcode(WPS_PREFIX.'-mail-post', 'wps_mail_post');`
 * However I’ve also tested it with a couple different shortcodes from different
   plugins. I have heard from another forum that the issue might be with the shortcode
   handler not being designed to work more than once per page. If there’s a limitation
   with the shortcode handler, how can that be overcome to display multiple shortcodes
   per page?
 * UPDATE: Replaced do_shortcode call with
 *  `<?php echo wps_mail_post(); ?>`
 * and that’s giving a missing argument error and also only working correctly (clicking“
   New Message” opens the message dialogue) for the top post at roadtripsharing.
   com/road_trip (the corresponding buttons on other posts only trigger page refresh
   and navigation to top of page).
 * Will look into the $args but what’s up with the functions only working for the
   first post / how to fix it? If the same issue is occuring with the function pasted
   in rather than using do_shortcode does that mean the issue is not with the shortcode
   handler after all?
 * Any tips for getting shortcodes to work for each rendered item in an archive,
   and not just the first one?

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years ago](https://wordpress.org/support/topic/shortcode-function-only-working-for-first-post-rendered-by-loop-archive-page/#post-7325826)
 * Without seeing the page in question, I can only speculate. (roadtripsharing.com/
   road_trip went 404 when I tried it)
 * The button more than likely triggers some javascript that displays the form. 
   The javscript is probably picking up the HTML element by ID. HTML standards only
   allow a single ID on a page. The shortcode handler is probably outputting the
   same ID for every post, so the javascript only works off the first ID found –
   that of the first post.
 * To work with multiple posts, the plugin would need to be altered to output unique
   IDs, and the javascript altered accordingly. It’s not a good idea to alter plugin
   code for the same reasons we should not alter core code. If the plugin author
   thoughtfully offered actions and filters with which you could tweak how the plugin
   works, then interfacing with the plugin that way would be ideal.
 * Otherwise, you are better off with your own code for this, even if that simply
   entails forking the plugin as your own variant. The drawback is if there are 
   any security holes in the original plugin, you inherit them and they do not get
   patched like the original plugin would hopefully be.
 * The javascript’s selection process, instead of by ID, is better with jQuery selectors.
   Then the code can operate on the appropriate HTML element with the `this` (selected)
   object.
 *  Thread Starter [andygo12](https://wordpress.org/support/users/andygo12/)
 * (@andygo12)
 * [10 years ago](https://wordpress.org/support/topic/shortcode-function-only-working-for-first-post-rendered-by-loop-archive-page/#post-7325982)
 * Of course, the IDs, that makes perfect sense thanks. Would need to write or hook
   in loop-friendly variants. Or decide “Do I really need that shortcode?” I changed
   the archive url to roadtripsharing.com/join-an-anazing-adventure and moved the
   shortcode (e.g. the button for sending a message) to single post template only.

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

The topic ‘Shortcode / function only working for first post rendered by loop archive
page’ is closed to new replies.

## Tags

 * [custom post](https://wordpress.org/support/topic-tag/custom-post/)
 * [do_shortcode](https://wordpress.org/support/topic-tag/do_shortcode/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [andygo12](https://wordpress.org/support/users/andygo12/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/shortcode-function-only-working-for-first-post-rendered-by-loop-archive-page/#post-7325982)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
