Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks I'll give it a go tomorrow and let you know how it goes –  toomanyairmiles Mar 5 '12 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? –  toomanyairmiles Mar 6 '12 at 0:05
    
Interactive Map is the page's HTML title by the way... –  toomanyairmiles Mar 6 '12 at 0:15
    
Forgot to add global $post before the foreach statement. –  Stephen Harris Mar 6 '12 at 0:24
    
As in 'global $post foreach($posts as $post):' this seems to cause an internal server error –  toomanyairmiles Mar 6 '12 at 0:26
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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