Integrating an interactive map into a WordPress custom post type

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

Read More

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

<!--nw-->
<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>

<!--ne-->
<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>        
</div>

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…

Related posts

Leave a Reply

1 comment

  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:

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

    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’.

      <?php
        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
            ));
            if(!empty($posts)){
                echo '<ul>';
                global $posts;
                foreach($posts as $post): 
                    setup_postdata($post); 
                    //Display post title
                    echo '<li>'.get_the_title().'</li>';
                endforeach; 
                echo '</ul>';
            }
        }
        ?>
    

    Please note this is untested.