我正在运行Wordpress WP_GeoQuery,按位置在自定义帖子类型内拉帖子,没有问题。但是,循环会中断页面上的其他HTML,包括侧栏&页脚。我使用了'posts_per_page'选项,似乎Wordpress在第61篇(我知道的随机数)帖子后死亡。
我意识到它可以被分页,这将解决这个问题,我将这样做,但好奇心已经占了我的上风,它一直困扰着我不知道为什么。我在谷歌上搜索了一下,但找不到任何答案。
所以,我基本上想知道Wordpress是否设置了61篇文章的限制,或者如果我做了一些非常愚蠢的事情,打破了它?下面的代码. .
提前感谢您的帮助!
<?php // get location of user
$url = "http://freegeoip.net/json/". $_SERVER['REMOTE_ADDR'] .'';
$geo = json_decode(file_get_contents($url), true);
// store lat and long of user location
$Slat = $geo[latitude];
$Slng = $geo[longitude];
?>
<?php $args = array(
'post_type' => 'event',
'latitude' => $Slat,
'longitude' => $Slng,
'author'=> $user_id,
'posts_per_page' => 61
)
?>
<?php $the_query = new WP_GeoQuery( $args ); ?>
<?php $plus = 0; ?>
<?php foreach( $the_query->posts as $post ) : ?>
<?php $postid = get_the_ID(); ?>
<div class="event clearfix">
<div class="event-image grid-1-4 no-padding">
<?php $images = get_field('images'); ?>
<img src="<?php echo $images[0][sizes][thumbnail]; ?>" alt="<?php the_title(); ?>" />
</div>
<div class="event-info grid-1-2">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<form action="/edit-event" method="POST">
<input type="hidden" value="<?php echo $postid; ?>" name="id" />
<input type="submit" class="edit-button" value="edit event" />
</form>
</div>
<div class="event-details grid-1-4 no-padding">
<div class="detail">Distance <?php echo round($post->distance, 1); ?> mi</div>
<div class="detail">Price: £<?php the_field('adult'); ?></div>
<?php include('countdown.php'); ?>
</div>
</div>
<?php $plus++; ?>
<?php endforeach; ?>
查看您的日志,在该问题之后抛出的错误/异常是什么?你看过第61条(也许是第62条)帖子吗?在显示第61个帖子时,分页是如何工作的?请提供更多信息
下面的修复以防其他人有这个问题。只需在可能没有值集的字段周围添加if语句。
<div class="event-image grid-1-4 no-padding">
<?php if( get_field('images') ) : ?>
<?php $images = get_field('images'); ?>
<img src="<?php echo $images[0][sizes][thumbnail]; ?>" alt="<?php the_title(); ?>" />
<?php endif; ?>
</div>