wordpress loop and multiple google maps getting errors in console - wordpress

I'm trying to create a slide out box with 4 localisations shown on maps. Each localisation on one map.
I have created a custom post type with custom post fields lat and lng. I'm getting the custom post fields through
<?php
$args = array(
'post_type'=> 'localisations',
'posts_per_page' => 4,
'orderby' => 'date',
'order' => 'DESC',
);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$meta = get_post_meta( $post->ID, 'real_estate_location_details', true );
?>
In my next step I'm adding to div.mapholder an ID with increment using jQuery
jQuery(function($){
var i=0;
$('.mapholder').each(function(){
i++;
var newID='map'+i;
$(this).attr('id',newID);
$(this).val(i);
});
});
<div id="map-container">
<div class="mapholder"></div>
</div><!-- #map-container -->
and at the end I'm creating the map
function initialize() {
var latVariable = "<?php echo $meta['lat']; ?>";
var lngVariable = "<?php echo $meta['lng']; ?>";
var mapOptions = {
center: {lat: latVariable, lng: lngVariable},
zoom: 5
}
var map = new google.maps.Map(document.getElementById('map'+i), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
the console outputs some errors:
`You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.` - I think its because the increment in `var map new google.maps.Map(document.getElementById('map'+i), mapOptions);` doesn't work, but I don't know how to fix this.
Uncaught SyntaxError: Unexpected token new - Got this one. I was missing `=` between map and new
Could you please help me out whit this?

You have the issues because you are adding Google Api scripts inside of the loop - that is why they are getting loaded so many times and you have the error displayed.
It would be hard to present you with working example without being able to actually run it so please do not treat it as a final working solution but rather as a direction.
First of all you do not need this script which creates the IDs as it doesn't help in your scenario and it can be done easily while running PHP loop.
Second thing would be to change the way the callback is called on window load. By defining the function in the loop you are actually redefining it each time.
Third thing is to extract google API script outside of the loop.
Please find the code in this pastebin.
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC5rsZHHAmzP_pOuHGKEUtarn2QideIyUM"></script>
<div id="real-estate-box" class="slideout">
<div class="real-estate-box-wrapper">
<p class="real-estate-box-title">For sale</p>
</div>
<div class="slideout-inner">
<?php
$args = array(
'post_type'=> 'real-estete',
'posts_per_page' => 4,
'orderby' => 'date',
'order' => 'DESC',
);
query_posts($args);
?>
<div class="see-all-real-estate col-xl-12">
<?php echo 'see all'; ?>
</div>
<ul>
<?php
$map_id = 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$meta = get_post_meta( $post->ID, 'real_estate_location_details', true );
?>
<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function() {
var map new google.maps.Map(document.getElementById('map-<?= $map_id ;?>'), {
center: {
lat: "<?= $meta['lat']; ?>",
lng: "<?= $meta['lng']; ?>"
},
zoom: 5
});
});
</script>
<li class="col-xl-3">
<a class="real-estate-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .real-estate-title -->
<div id="map-container">
<div class="mapholder" id="map-<?= $map_id ;?>"></div>
</div><!-- #map-container -->
see more
</li><!-- .col-xl-4 -->
<?php
$map_id++;
endwhile; ?>
</ul>
<?php
else:
echo '<p class="no-real-estete">We don\'t have any real estates for sale on the moment</p>';
endif;
?>
</div>
</div>
https://pastebin.com/5Tz9Ueaa
Actually I would strongly recommend going in completely other direction with your code as you will have some issues with that approach.
Nevertheless code form pastebin should work in your case.

Related

Wordpress - get pages in same page

I'm creating a wordpress top-down website by sections and i don't know which is the best way to get the pages into different sections.
I also want to take into account the charging performance
<section id="about">
<!-- Content of page -->
</section>
<section id="services">
<!-- Content of page -->
</section>
<section id="contacts">
<!-- Content of page -->
</section>
Thank's
I would use a simple WP_Query instance here, and use the following code:
<?php
// Set the WP_Query arguments
$args = array(
'post_type' => 'page',
// You need to change this to match your post IDs
'post__in' => array( 10, 20, 30 ),
'order_by' => 'post__in',
'order' => 'DESC' // Might need to change this to "ASC"...
);
// Create the page section query
$page_section_query = new WP_Query( $args );
// Simple check...
if( $page_section_query->have_posts() ) :
while( $page_section_query->have_posts() ) :
$page_section_query->the_post();
global $post;
// Print out the section!
?>
<section id="<?php echo esc_attr( $post->post_name ) ?>" <?php post_class( array( esc_attr( 'page-section-' . $post->post_name ) ) ); ?>>
<!-- contents of the page section -->
</section>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Simple and effective, 1 query is all that this needs. Just keep adding more post IDs if you want more sections.
If you want to avoid using post IDs as an ordering parameter you could use: menu_order instead. And if you would like to avoid using post__in parameter you could add all the pages to a page parent and use the parent post ID and get all the page children for the sections. This would make the solution a little more modular.
Read more about WP_Query here: https://codex.wordpress.org/Class_Reference/

How do I get the latest post in the selected language with qTranslate-x?

The setting in the settings=>languages for "Hide Content which is not available for the selected language." is unchecked. This is the preferred state for the whole site, but for some posts I would like to show only the latest post in the selected language. (So without the default behavior: "Sorry, this entry is only available in French.").
So far I have this code, which shows the latest post in the language it is written in, but I would like to get only the posts written in the selected language.
while ( have_posts() ) : the_post();
$mypost = get_post(get_the_ID());
$content = qtranxf_use('en', $mypost->post_content,false);
echo "$content";
endwhile;
So in the end I've used this approach to query on a specific language:
$mypost = array('post_type' => 'posts', 'paged' => get_query_var('paged'), 's' => '[:en]', 'posts_per_page' => 7);
It adds an extra query for the keyword: [:en] or any language you want. And than you can just loop through it:
$loop = new WP_Query($mypost);
while ($loop->have_posts()) : $loop->the_post(); ?>
<article>
<?php the_content(); ?>
</article>
<?php
endwhile;

Google material design in dynamic page

So, I am using infinite scroll (http://infiniteajaxscroll.com/docs/getting-started.html).
However when the main content is inside of "mdl-js-layout" the infinite scroll function does not work (shown below)
So, if I remove the mdl-js-layout in the most outer div, then infinite scroll works otherwise it does not work.
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-drawer">
<header class="mdl-layout__header">
Head
</head>
<main class="mdl-layout__content">
<div class="rfp_hide" id="rhm_post_show">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'paged'=>$paged,
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
global $post, $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="rhm_post_container">
Post
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
</main>
</div>
<script>
var container = document.querySelector('#rhm_post_show');
var msnry = new Masonry( container, {
// options
itemSelector: '.rhm_post_container',
gutter: 10
});
var ias = $.ias({
container: '#rhm_post_show',
item: '.rhm_post_container',
pagination: '#rh_nav_below',
next: '.rh_nav_next a',
delay: 100
});
ias.on('render', function(items) {
$(items).css({ opacity: 0 });
});
ias.on('rendered', function(items) {
msnry.appended(items);
});
ias.extension(new IASSpinnerExtension());
ias.extension(new IASNoneLeftExtension({html: '<div class="ias-noneleft" style="text-align:center"><p><em>You reached the end!</em></p></div>'}));
</script>
I asked the developer and got the following response:(https://github.com/google/material-design-lite/issues/1584#issuecomment-139060676)
MDL is targeting static content for now, so use-cases with infinite scroll are not supported by us right now. However, there are people who have made MDL work with dynamic sites and you can probably get valuable help by heading over to StackOverflow and using the material-design-lite tag.
Does anyone what I should try?
Thanks!
I also had some issues with dynamic websites and mdl.
On their website I found the following paragraph about dynamic websites, which helped in my case:
https://getmdl.io/started/index.html#dynamic
Material Design Lite will automatically register and render all elements marked with MDL classes upon page load. However in the case where you are creating DOM elements dynamically you need to register new elements using the upgradeElement function.
They also provide an example, how to use the function.
This solved the issue on my side:
Material design locks the main wrappers height to 100%;
.mdl-layout__container .mdl-js-layout
this avoid masonry to keep loading more elements
so in your custom CSS add:
.mdl-layout__container, .mdl-js-layout{
height: auto;
}

add more content to infowindow in google maps api

I am using Advanced Custom Fields to set the location of a post and display it on a map within my custom post template and it works great. But I have also created a google map that plots all the locations of my custom posts types on one map.
My problem is that I can’t get it to pull content or a link into the infowindow from those custom posts. I also can't get it to change the marker depending on a true/false field.
I’ve looked around the internet and tried to use the code that google suggests as well as the users on this site, but I seem to be doing something wrong. All it does is display the title followed by a weird symbol. Any help would be appreciated :)
This is the code I am using:
<section>
<?php
$args = array(
'post_type' => 'offices',
'posts_per_page' => -1
);
// query
$wp_query = new WP_Query( $args );
$NUM = 0;
?>
<div id="map"></div>
<script src='http://maps.googleapis.com/maps/api/js?sensor=false' type='text/javascript'></script>
<script type="text/javascript">
var locations = [<?php while( $wp_query->have_posts() ){
$wp_query->the_post();
$location = get_field('location'); // IMPORTANT << Change this for your Google map field name !!!!!!
?>
['<?php the_title(); ?>', <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?> ],
<?php } ?> ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7, /*Here you change zoom for your map*/
center: new google.maps.LatLng(42.6, -76.0), /*Here you change center map first location*/
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</section>
Yes, I got your problem. As per me you are doing well but you need to modify your code slightly.
Just make an array of values in your while loop like for markers :
$marker[] = '"'.get_the_title().'"'.','.$event_lat.','.$event_long;
and for marker details like :
$marker_details[] = get_the_title().'_sunil_'.get_the_content($post->ID).'_sunil_'.$ev_location.'_sunil_'.get_permalink($post->ID);
Then after this just place your script using foreach loop and explode above $marker_details array by separator sunil.
The code will be :
var markers = [
<?php foreach ($marker as $mar){
echo "[".$mar."],";
};?>
];
var infoWindowContent = [
<?php foreach ($marker_details as $mar_det){
$check_val = explode('_sunil_',$mar_det);?>
['<div class="info_content">' +
'<h3 style="display:block"><?php echo $check_val[0];?></h3><strong style="display:block;text-transform:capitalize;"><?php echo "Location : ".$check_val[2];?></strong>' +
'<p><?php echo substr($check_val[1], 0, 200);?>...</p>Read More' + '</div>'],
<?php }?>
];
Do not disturb structure by even a single comma while making it dynamic using loops. Just take a fresh html and implementation instructions from here : http://www.wptricks24.com/map-with-multiple-markers

Final posts not showing on last category page on wordpress site

I have a WordPress site that has a ton of posts on it, all categorized. I set up a new theme, with pagination (15 posts per page), so the user can cycle through each page. Some of the categories paginate fine. Others are missing the final page.
So, if a category has 66 posts ... the first 4 pages show 15 different posts. However, when I click to view page 5, the page says "no posts found". Where did the last 6 posts go? They still show up in my administration (as published and visible). However, other category pages do not have this issue - for example, I have a category with 42 post, and it has 3 page ... the last page of which has 12 of the final post.
So, the pagination seems to be working fine (since it clearly shows the correct number of pages, for the number of posts). Please take a look below at the code I have... this is code from my templated index.php page (I didnt set up a category.php page, because it lists very similarly to the homepage).
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Page
$args = "posts_per_page=15&paged=".$paged; // Base query
$category = get_queried_object(); // Get Cat info
$thisCat = $category->term_id; // Get Cat ID (if exists)
$tagID = get_queried_object()->term_id; // Get Tag ID (if exists)
echo '<!-- paged: '.$paged.'-->';
echo '<!-- catID: '.$thisCat.'-->';
echo '<!-- tagID: '.$tagID.'-->';
if (is_home() || is_front_page()) { // HOMEPAGE
query_posts($args.'&orderby=rand');
echo '<!-- args: '.$args.'&orderby=rand'.'-->';
} elseif ( is_search() ) { // SEARCH RESULTS ?>
<?php
$search_query = get_search_query();
query_posts($args.'&s='.$search_query);
echo "<!-- args: ".$args.'&s='.$search_query."-->"; ?>
<h1>Search</h1>
<div class="content_labels">
<div class="content_label">SEARCH RESULTS FOR: <?php echo $s; ?></div>
</div>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
<< Previous Page
</div><?php
} elseif( is_category() ) { // CATEGORY
query_posts($args.'&cat='.$thisCat);
echo '<!-- args: '.$args.'&cat='.$thisCat.'-->'; ?>
<div class="content_labels">
<div class="content_label">Category:</div>
</div>
<h1><?php single_cat_title( '', true ); ?></h1>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
<< Previous Page
</div><?php
} elseif( is_tag()) { // TAGS
echo '<!-- args: '.$args.'&tag_id='.$tagID.'-->';
query_posts($args.'&tag_id='.$tagID); ?>
<div class="content_labels">
<div class="content_label">Tag:</div>
</div>
<h1><?php single_tag_title(); ?> </h1>
<div class="clear" style="margin:0 0 10px 0;"></div>
<div class="previouspage">
Previous Page
</div><?php
}
if ( have_posts() ) :
$i=1;
while ( have_posts() ) : the_post(); ?>
// PAGE CODE GOES HERE
endwhile; ?>
<?php base_pagination(); // PAGINATION CODE ?>
<?php endif; ?>
Here is the pagination code, from my functions.php ... I don't think this is the issue...
function base_pagination() {
global $wp_query;
$big = 999999999; // This needs to be an unlikely integer
// For more options and info view the docs for paginate_links()
// http://codex.wordpress.org/Function_Reference/paginate_links
$paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 5
) );
// Display the pagination if more than one page is found
if ( $paginate_links ) {
echo '<div class="pagination">';
echo $paginate_links;
echo '</div><!--// end .pagination -->';
}
}
Can anyone see what is going wrong? I've been playing with this for several hours, and I can't seem to find a solution...
So, I did some digging, and tried some things... I couldn't find anything wrong with the code above. The query was correct (showing 15 per page, for each category archive page, etc.), and the pagination was working ... In the end, the default POSTS PER PAGE was conflicting with my own posts_per_page=15 query. Not sure WHERE this conflict was occurring (that is beyond my skills) - but I did learn how to stop it.
Under SETTINGS - READING ...
I just changed the "Blog pages show at most" to "15"
This wasn't an ideal fix (since I don't know where this problem started, and I can't adjust custom "posts_per_page" if it differs from 15)... but my site now works how I want.

Resources