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.

I'm creating a custom Google map that plots multiple markers.

View the link as it will make it easier to explain what is happening and what I want

If you click on each marker it shows company names that are grabbed from the child pages. At the moment it's showing ALL the company names on each marker. How can I show just one company name per marker?

Here's the code controlling the little popup:

$pages = get_pages(array('child_of' => 1873, 'sort_column' => 'menu_order')); 
$counter = 1;
foreach($pages as $post) {
    setup_postdata($post); 
    $fields = get_fields();
    ?> 
    <p><?php $counter++; echo $fields->company_name;?></p>
    <?php
}
wp_reset_query();

Once it's looped through once, the next time it loops through I need it to start on the next child and not show the first one.

share|improve this question
    
Please consider re-working your Q. What's a dot, info per one "plotted" dot, etc.? –  kaiser Sep 21 '11 at 15:37
    
@kaiser Just edited it now. –  Rob Sep 21 '11 at 15:40
    
Hey Rob. Still don't know what's a dot... writing bold doesn't change too much... –  kaiser Sep 21 '11 at 17:03
    
@kaiser I've posted a link in the question that will explain it all. –  Rob Sep 21 '11 at 18:30
    
This is a general PHP question, not a WordPress question. –  Chip Bennett Sep 22 '11 at 16:06
add comment

2 Answers 2

up vote 2 down vote accepted

Use the offset argument, and only get 1 result:

global $my_page_offset;

if(!isset($my_page_offset))
  $my_page_offset = 0;

$pages = get_pages(array(
  'child_of'    => $post->ID, 
  'sort_column' => 'menu_order', 
  'offset'      => $my_page_offset, 
  'number'      => 1,
));

$my_page_offset++;

...

I'm assuming here that you don't have much control over these loops, because you're doing them multiple times. This is why the global variable...


Last try:

After var infowindow; add this:

<?php

$pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
$data = array();
foreach($pages as $post){
  setup_postdata($post);
  $fields = get_fields(); 
  $data[] = '<p>'.$fields->company_name.'</p>';
}
wp_reset_query();

// the js array
echo 'var marker_data = ["'.implode('","', $data).'"];';
?>

Now infowindow.setContent should be:

infowindow.setContent(marker_data[i]);
share|improve this answer
    
Thanks for the attempt. It doesn't seem to echo out anything now. –  Rob Sep 21 '11 at 15:49
    
Changing the offset to 1 made the first child page appear on all items. –  Rob Sep 21 '11 at 15:53
    
what happens if you remove the number argument ? –  onetrickpony Sep 21 '11 at 15:57
2  
Ah I misunderstood your question. That function is part of Google Maps :) You should post the entire javascript code, so we can see how are your markers created. –  onetrickpony Sep 21 '11 at 16:12
1  
The problem is that the gmaps adds the field that's generated with PHP trough a loop, and applies it to all elements. Post the entire code, especially the one around places.push(new google... (I guess you have another PHP foreach loop there?). You'll need to create a javascript array with fields for each element, and pass that to infowindow.setContent –  onetrickpony Sep 21 '11 at 18:44
show 12 more comments

Figured out that the php within the JS needed to be json encoded:

echo 'var marker_data = ' . json_encode($data) . ';';
share|improve this answer
    
...which (again) demonstrates that this question didn't have anything directly to do with WordPress. –  Chip Bennett Sep 23 '11 at 13:47
add comment

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.