摆脱尾随逗号(PHP)

时间:2012-10-12 12:45:36

标签: php

最后一行是它无法正常工作的地方。我无法弄明白应该去哪里而不是$post

    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);
        }
    }

1 个答案:

答案 0 :(得分: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);