Integrating an interactive map into a WordPress custom post type

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.

Leave a Comment