2012-10-12 156 views
-3

最後一行是它不起作用的地方。我不知道應該去那裏,而不是$post擺脫尾隨逗號(PHP)

if (isset($_COOKIE["WP-LastViewedPosts"])) { 
    //echo "Cookie was set.<br/>"; // For bugfixing - uncomment to see if cookie was set 
    //echo $_COOKIE["WP-LastViewedPosts"]; // For bugfixing (cookie content) 
    $zg_post_IDs = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))); // Read serialized array from cooke and unserialize it 
    foreach ($zg_post_IDs as $value) { // Do output as long there are posts 
     global $wpdb; 
     $zg_get_title = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE ID = '$value+0' LIMIT 1"); 
     foreach($zg_get_title as $post) { 
      echo "<a href=\"". get_permalink($value+0) . "\" title=\"". apply_filters('the_title',$post->post_title) . "\">". apply_filters('the_title',$post->post_title) . "</a> \n"; // Output link and title 
      echo implode(', ', $post); 
     } 
    } 
+0

你到底想幹什麼?需要詳細說明*如何*它不工作。顯示'$ post'的輸出。 –

+2

在我看來,你根本不需要那條線。 –

+0

我想在鏈接後顯示逗號,除了最後一個。 與上述,它根本不顯示逗號。 – Phantasmix

回答

2

根據您的意見這樣做的,一個辦法:

foreach ($zg_get_title as $post) 
{ 
    $title = apply_filters('the_title', $post->post_title); 
    $posts[] = '<a href="'.get_permalink($value).'" title="'.$title.'">'.$title.'</a>'; 
} 

echo implode(', ', $posts); 
+0

僅供參考原始問題中發佈的代碼片段是__HORRIBLE__ –

+0

Hi @Matt Humphrey,您的代碼正確顯示逗號,但重複發佈兩次。 – Phantasmix