So I have a loop of a custom post type. It looks for the settings of each custom post and creates a shortcode with the information stored in the db.

It works perfectly for when the user saves one post. When they add a second it works, however overwrites the first.

ex: lets say [first] shows a star. When I add another post and give it [second] to show a square – the [first] shortcode ends up showing a square even though the name is wrong.

Does this make sense?

CODE:

  $ids = array();
  $args = array( 'post_type' => 'custom_post');
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
  array_push( $ids, get_the_ID() );
  endwhile;
  foreach (array_unique($ids) as $key => $value) {


  $get_custom_shortcode_name = $titan->getOption( 'custom_shortcode_name', $value ); // this gets the shortcode name stored in the database
  $GLOBALS['get_js_data'] = $titan->getOption( 'additional_javascript', $value  ); // this gets the js the user has added to the custom post in the database

  ${"{$get_custom_shortcode_name}_fn"} = function($atts) {
  ob_start();
?><script><?php echo $GLOBALS['get_js_data']; ?></script><?php
  return ob_get_clean();
};

add_shortcode($get_custom_shortcode_name, ${"{$get_custom_shortcode_name}_fn"});
}

Thanks for the help

Read more here: Loop not making separate short-codes – it overwrites the first


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: