2012-01-24 11 views
0

可能重複:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectarray_pop()預計參數1通過分類代碼要在WordPress的相關的帖子陣列

警告:array_pop()預計參數1是陣列,null給出...在線34

我得到以下錯誤whe n使用以下功能。該函數的輸出按預期工作,除了在輸出之前顯示錯誤。

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


更新:

上面的代碼是相當不完善。這個插件完美的作品:

http://pippinsplugins.com/write-a-better-related-posts-plugin-for-custom-taxonomies/

回答

1

的問題是,你使用array_pop的潛力排列的指標。 array_pop被設計爲彈出數組末尾的最後一個元素。

+0

感謝弗朗西斯 - 那麼解決方案是什麼? –

+0

我不確定;我會張貼,如果我知道:)你想用它做什麼?你想抓住數組中的單個元素嗎? –

+0

另外,電位數組看起來像什麼? –

1

array_pop()接受一個數組作爲參數,它從數組中刪除最後一個值(返回它)。如果您想獲得該數組元素的值,你應該這樣做

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

如果您還需要從數組中刪除,那麼你需要的是這樣的:

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

感謝,熵 - 從邏輯上看,它應該是完美的工作,但我認爲我使用的代碼是相當不完善的,並將該頁面引入到具有該代碼的無限循環中。我結束了與另一個插件,發現在這裏http://pippinsplugins.com/write-a-better-related-posts-plugin-for-custom-taxonomies/ ...完美作品 –

+0

我認爲這是因爲'$ count '並不總是正確地遞增,所以在某一時刻它會產生一個循環。 – entropid

相關問題