This is basically a php issue as I’m fairly new to it and can’t figure out how to code things the way I want. I am basically trying to pull information from custom fields (grid reference) and post titles etc to then write out a (long) shortcode. This shortcode creates a map (OS OpenSpace Maps Plugin)
Currently I have successfully got code to print out (echo) the shortcode, and then I have to edit it slightly, removing spaces and a few obsolete characters, then manually paste this back into template using ‘do_shortcode’
The correct format of the shortcode is:
[osmap markers="HU123456;<a href='http://example.com'>Link 1</a>|HU123456;<a href='http://example.com'>Link 2...etc"]
My current code to print on the screen is:
<?php
echo '[osmap width="800" height="600" markers="';
$args=array(
'post_type' => 'sport-crag',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo get_post_meta($post->ID,'gridref', true);?>;<a href='<?php the_permalink() ?>'><?php the_title(); ?></a>|
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
echo '"]';
?>
What I really want to do is put all this inside the ‘do_shortcode’ function. There are also a few issues with my solution so far that I can’t figure out how to sort.
-
Grid references often have spaces in them (e.g. HU 123 456), but this will not work in the shortcode so they need the spaces taking out (HU123456).
-
My code puts a final ‘|’ after the last entry which needs to be removed. Not sure how to do this though as its part of the while loop.
Any help to get this an automatic process all inside the php would be much appreciated! It only takes 5 mins at the moment, but the issue is that if any other posts are added, the process has to be redone before they appear on the map, so it will get annoying. I also have 4 maps for different post types so it will get very irritating!