array_pop()期望参数1在Wordpress相关文章中按分类代码排列


array_pop() expects parameter 1 to be array in Wordpress related posts by taxonomy code

警告:array_pop()要求参数1为数组,在中给定null。。。在线34

使用以下函数时,我会出现以下错误。函数的输出按预期工作,但错误显示在输出之前。

<?php 
global $post;
//Get the terms for the current post
$terms = get_the_terms( $post->ID , 'character', 'string');
echo $terms[1];
$potentials = array();
$c = 0;
$totalFound = 0;
//Gather your posts for each term
foreach($terms as $term){
   $q = array(
    'character' => $term->slug, //term to retrieve from custom taxonomy
    'numberposts' => 3,  //limit to 4 posts
    'post_type' => 'videos', //get only posts
    'exclude' => $post->ID //exclude current post
   );
   $posts = get_posts($q);
   $totalFound+= count($posts);
   $potentials[$c++] = array_reverse($posts);
}
$count = 0;  //The number of good posts we've found
$index = 0;  //Number of posts we've tried
$max = $totalFound > 3 ? 3 : $totalFound;  //The max we can find
$posts = array();
//Now pick one post from each term until we reach our quota,
//or have checked them all
while($count < $max){
  //Pop off a post to use
  $rpost = array_pop($potentials[$index++]);
  //if we got a post (if there was one left)
  if($rpost){
    //don't take duplicates
    if(!isset($posts[$rpost->ID])){
      $posts[$rpost->ID] = $rpost;
      $count++;
    }
  }
  $index = ($index % 3); //rotate through the 4 term arrays
}
foreach($posts as $post){
    setup_postdata($post);
    $exclusive = get('aoexclusive_yes');
    if(!$exclusive) {$exclusive = null;} else {$exclusive = 'exclusive';}
    $image = wp_get_attachment_image_src( get_post_thumbnail_id(  $post->ID ), "video-thumb" );
?>
                    <div class="thumb-post<?php echo ' '.$exclusive; ?>">
<?php if($image) { ?>
                        <a class="featured-image" href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" /></a>
<?php } else { ?>
                        <a class="featured-image" href="<?php the_permalink(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/assets/images/default.jpg" /></a>
<?php } ?>
                        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php ao_post_meta(); ?>    
                    </div>
<?php } ?>

有人有这个错误的经验吗?或者看看是什么原因造成的?

代码最初来自http://wordpress.org/support/topic/custom-taxonomy-related-posts-query

问题是您在潜在数组的索引上使用array_pop。array_pop设计用于弹出数组末尾的最后一个元素。

array_pop()接受一个数组作为参数,并从数组中移除最后一个值(返回它)。如果你想得到这个数组元素的值,你应该做一些类似的事情

$rpost = $potentials[$index++];

如果你还需要将其从阵列中删除,那么你需要这样的东西:

$rpost = $potentials[$index];
unset($potentials[$index]);
$index++;