A while ago I created an interactive map for my website using Ian Lunn's BBC News Map, and custom fields to set the html for each area. It looks like this:-

enter image description here

I'm now trying to integrate this with a custom post type, the idea is that each map area would become a category with a list of posts in that category underneath (item 1, item 2 in the picture).

I've found examples of displaying a custom post type by category, but my template looks like this

<div id="nw" class="counties-container">
<h2>North West</h2>
<p class="nw-t"><?php if($data = get_post_meta($post->ID, 'North-West', true)) { echo $data; } ?></p>

<div id="ne" class="counties-container">
<h2>North East</h2>
<p class="nw-t"><?php if($data = get_post_meta($post->ID, 'North-East', true)) { echo $data; } ?></p>        

Requiring custom code for each section. Is there an approach which would allow me to display the posts by category and keep the template code the way it is now, or should I rewrite the template so a single template can display all the areas.

Thanks in advance...

1 Answer 1


The easiest way would be to use a function which accepts a region taxonomy term as an argument, and outputs a list of posts with that term (i.e. 'in that region'). So your template looks like:

<div id="nw" class="counties-container">
<h2>North West</h2>
<p class="nw-t"><?php my_posts_by_region('nw'); ?></p>

I'm assuming that 'nw' is the term name (slug) of your North West region. Then you need to define the function (I'd recommend creating your own plug-in for this function, but if you must, you can use functions.php):

Below I assume your posts are of type 'cpt_name' and the region taxonomy name is 'region'.

    function my_posts_by_region($term=''){
         //select (5 say) posts in this region (term), of some custom post type
         $posts = get_posts(array(
            'post_type' => 'cpt_name',
            'taxonomy' => 'region',
            'term' => $term,
            'numberposts' => 5
            echo '<ul>';
            global $posts;
            foreach($posts as $post): 
                //Display post title
                echo '<li>'.get_the_title().'</li>';
            echo '</ul>';

Please note this is untested.

  • Thanks I'll give it a go tomorrow and let you know how it goes Mar 5, 2012 at 0:39
  • it works, or at least it doesn't return errors, but oddly it only prints the words 'interactive map' rather than the post title, i checked the db queries and all seems to work correctly, I've updated my question with the query code I'm using - any thoughts? Mar 6, 2012 at 0:05
  • Interactive Map is the page's HTML title by the way... Mar 6, 2012 at 0:15
  • Forgot to add global $post before the foreach statement. Mar 6, 2012 at 0:24
  • As in 'global $post foreach($posts as $post):' this seems to cause an internal server error Mar 6, 2012 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.