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? i.e So one say "MediWales" and the other says "Teamworks Design & Marketing", and so on when I add more companies.
Here's the code controlling the little popup:
<?php $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.
UPDATE:
It seems like it's very close, it's showing one company but the same one on both markers.
<?php
$counter = 1;
$pages = get_pages(array('child_of' => 1873, 'sort_column' => 'menu_order', 'offset' => $counter, 'number' => 1));
foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php echo $fields->company_name; echo $counter; ?></p>
<?php $counter++; }
wp_reset_query(); ?>
Your issue is the fact that PHP is by nature preprocessed. JavaScript doesn't run that PHP everytime you click that marker.
Your best bet is to output a JSON object from PHP containing all the markers and their attributes, then parse it dynamically with JavaScript.
Related
Example:
We got some nice posts that got a link to jump to the next post with scrolling down the page. No reload, new tab or anything just a simple smoothscrolling of the page.
Problem:
We are inside a Wordpress Loop that creates some content from the DB and want to generate a link that jumps to the next run-through that will be generated by the Loop. Within the loop we cant predict what will be the next post/product or whatever post-type the loop should go through. So how am I giving the Link the correct anchor link?
Possible Solutions:
Maybe we can store the data from each run in an array?
Maybe we should create a second loop that only saves the data?
Maybe I should just use JS to rename that anchors after it finishes...
You can add an incremental id with the loop index and assign the anchor to the current index + 1. A simple example:
<?php
global $wp_query;
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$index = $wp_query->current_post + 1;
?>
<div id="my_post_<?php echo $index; ?>"><?php the_title(); ?></div>
<?php
if (($wp_query->current_post +1) <= ($wp_query->post_count)) {
echo 'Next post';
}
?>
}
}
?>
I have a Wordpress site and I am trying to fetch posts from a specific category. That works, but now I want to link to the specific posts with a permalink. I could not getting it to work with escaping in a foreach. Can someone tell me/show me what I am doing wrong here?
<?php
global $post; // required
$args = array('category' => 9); // include category 9
$custom_posts = get_posts($args);
foreach($custom_posts as $post) :
setup_postdata($post);
echo "<a href='.the_permalink().'> the_title() </a> ";
the_excerpt();
//and so on..
endforeach;
?>
You're not actually closing your quotes properly and aren't using the correct functions. Line 5 should read more like:
the_title("<a href='".get_permalink()."'>",'</a>',true);
BOOM.
I store post IDs in an array. I would like to loop through the array and display the IDs within a <div> containing <p> and <ul> tags, but only when at least one ID is in the array. If the array is empty no html can be returned. This implies that I should use some kind of if statement before the loop. Needless to say, my php skills are pretty basic and after two days of trying hard I am getting nowhere. Grateful for help!
My code (using Wordpress)
$postids = array();
...
$postids [] = $post->ID; //stores the post IDs in the array
Here is an update. I apologize for posting all this code as its quite messy with many things going on. It's the second loop of three (or more). The IDs displayed in a near identical first loop have been passed on. Only those IDs which have not been retrieved by the previous loop are displayed in order not to show any duplicate posts.
I have tried to remove all HTML markup and query the $postids with a new WP_Query after but that retrieves all posts I have ever created. I am pretty sure that's the right way to continue although I am obviously doing something wrong.
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[1]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5, //Display this number of related posts
'ignore_sticky_posts'=>1
);
$postids = array();
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul id="relatedposts">';
while ($my_query->have_posts()) : $my_query->the_post(); if (!in_array($post->ID, $ids)) {; $postids [] = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php }
$ids[]= $post->ID;
endwhile;
}
}
?>
</ul>
<?php if ($postids){ //$postids has at least one value set
echo '<div>Related posts</div>'; //Outputting the header text. This works! If there are no IDs in the array nothing is shown.
};
?>
This should work:
<?php
// assuming you have an array of ids called $postids
if(count($postids)){
echo "<div><ul>";
foreach($postids as $id){
echo "<li>$id</li>";
}
echo "</ul></div>";
}
?>
To break it down:
if(count($ids)){
count() returns the number of elements in the array $ids. Any number other than zero will evaluate to true and enter the if statement, zero will evaluate to false and the whole thing will be skipped.
foreach($ids as $id){
This loops through each element in the array $ids and assigns it to the variable $id. Hopefully the echo statements are self explanatory.
There are a couple of ways to do it.
if ($postids){ //$postids is TRUE (ie $postids is not an empty array)
//do your output
}
OR
if(count($postids) > 0){ //$postids has at least one value set
//do your output
}
Getting used to simple tests of true and !false is your friend
Mayhaps something like this? You should customize this code to retrieve the contents of the posts, or whatever you'd like to do.
<?php
$postIds = array(1, 2, 3, 4, 5);
?>
<html>
<head>
<title>Post IDs!</title>
</head>
<body>
<h1>Post IDs!</h1>
<?php if(empty($postIds)): ?>
<p>There are no post IDs :(</p>
<?php else: ?>
<ul>
<?php foreach($postIds as $postId): ?>
<li><?php echo $postId; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>
Thanks to the great help by Gordon, I now have a working solution. All html is removed from the messy original code above. The following if statement and foreach loop echoes out the html in a simple and convenient way. Styling the and tags is now very straightforward.
<?php
if(count($postids)){
echo "<div>Related posts<ul>";
foreach($postids as $id){
echo '<li>'.get_the_title( $id ).'</li>';
}
echo "</ul></div>";
}
?>
I am trying to write a function that will output every other attachment caption and format each nicely within a div containing sequential IDs. However, anytime I add an echo and echo before and after a post_excerpt my code is completely breaking. Based on my HTML output the first echo seems to be ignored until the second time through the loop. The captions seem to post fine if I take out the echo div code.
<?php while ( have_posts() ) : the_post(); ?>
<?php
$argsThumb = array(
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'attachment',
'post_status' => null,
'post_parent' => $post->ID,
'include' => $thumb_id
);
$thumb_images = get_posts($argsThumb);
$currentThumb = 1;
$captionID=1;
foreach ($thumb_images as $thumb_image) {
if ($currentThumb % 2 == 0)
echo '<div class="caption'.$captionID++.'">';
echo $thumb_image->post_excerpt;
echo '</div>';
$currentThumb++;
}
?>
<?php endwhile; // end of the loop. ?>
Here is my HTML Output, in theory there should be 5 captions based on the fact that I have 10 images and the code is suppose to pull every other image caption:
<div class="caption1"></div>
BEFORE 3</div>
<div class="caption2">CLIENT: ELYSIAN<br>AGENCY: IN HOUSE<br>PHOTOG: JEFF SCIORTINO<br><br><p>THIS IMAGE WAS PART OF A SERIES SHOT FOR THE ELYSIAN. I WAS APPROACHED TO ADD ELEMENTS THAT WERE NOT POSSIBLE TO CAPTURE IN CAMERA.</p></div>
AFTER 2.</div>
<div class="caption3">BEFORE 1</div>
It looks like one problem might be that you've forgotten to wrap the if block in braces.
Right now, what you have is basically this:
foreach ($thumb_images as $thumb_image) {
if ($currentThumb % 2 == 0) {
echo '<div class="caption'.$captionID++.'">';
}
echo $thumb_image->post_excerpt;
echo '</div>';
$currentThumb++;
}
So, you may want to make it look like this, intead:
foreach ($thumb_images as $thumb_image) {
if ($currentThumb % 2 == 0) {
echo '<div class="caption'.$captionID++.'">';
echo $thumb_image->post_excerpt;
echo '</div>';
$currentThumb++;
}
}
This above error is a really good reason to always include braces, even for single-line if blocks.
However, I don't think this will fully fix your issue. I suggest you look at your data again, as you can see that echo '</div>'; is happening five times. My guess is that the foreach ($thumb_images as $thumb_image) loop only has five attachments to grab.
I've setup a Google map that has several plotted markers, each one when clicked on has a little popup with info within.
I'm using the following code to show the company name per plotted marker, but it's going through the foreach loop and showing ALL the company names within one popup.
How can loop through and show one company name per plotted marker? I need it to show the first company in the list on the first marker, the second company in the list on the second marker and so on.
// Setting the content of the InfoWindow
infowindow.setContent('
<?php $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order')); foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php echo $fields->company_name; ?></p>
<?php } wp_reset_query(); ?>
');
Look at your code. what a mess! You would never have missed these obvious mistakes, if you would care to structure your code well! Don't write all your code on one line! Use best practice for code style!
Don't put all that PHP code inside the javascript function.
Instead, use a variable $contentMarkup, store everything in there and in the end echo this variable into the javascript code.
<?php
$contentMarkup = '';
//do all your stuff like
$contentMarkup .= '<p>';
$contentMarkup .= $fields->companyName;
$contentMarkup .= '</p>';
?>
infowindow.setContent('<?php echo $contentMarkup; ?>');
Regarding your actual question: If you want to generate more than one tooltip/window/younameit, they have to have unique identifiers so you can create one for each company. But to say more I'd need to know a bit more about what exactly you're trying to do. What information is available and from what source.
show what you want based on if statement inside foreach loop