array_pop() 期望参数 1 是按分类代码在 Wordpress 相关帖子中的数组

发布于 2024-12-28 21:24:22 字数 2586 浏览 2 评论 0原文

警告:array_pop() 期望参数 1 为数组,第 34 行 ... 中给出 null

在使用以下函数时出现以下错误。该函数的输出按预期工作,只是错误出现在输出之前。

<?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/自定义分类相关帖子查询

Warning: array_pop() expects parameter 1 to be array, null given in ... on line 34

I get the following error when using the following function. The output of the function works as expected except the error shows up before the output.

<?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 } ?>

Does anyone have any experience with this error or see what might be causing it?

Code originally came from http://wordpress.org/support/topic/custom-taxonomy-related-posts-query

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海的爱人是光 2025-01-04 21:24:22

问题是您在电位数组的索引上使用 array_pop 。
array_pop 旨在弹出数组末尾的最后一个元素。

The problem is that you're using array_pop on an index of the potentials array.
array_pop is designed to pop the last element off the end of an array.

array_pop() 接受一个数组作为参数,并从数组中删除最后一个值(返回它)。如果你想获取该数组元素的值,你应该这样做,

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

如果你还需要从数组中删除它,那么你需要这样的事情:

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

array_pop() accepts an array as argument and it remove the last value from the array (returning it). If you want to get the value of that array element, you should do something like

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

If you also need to remove it from the array, then you need something like this:

$rpost = $potentials[$index];
unset($potentials[$index]);
$index++;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文