dpr26232 2015-06-17 21:28
浏览 44
已采纳

根据数组中的时间戳存储发布数据

I have been trying to create a structural format of posts with the help of Wordpress get_posts function. The structure with I am creating is based on the post timestamps. So basically the posts having the same timestamps will be collected together and displayed on the page. The structure is something like

  1. TIMESTAMP (Example 12345678)

    POST 1 with timestamp 12345678

    POST 2 with timestamp 12345678

  2. TIMESTAMP (Example 87654321)

    POST 1 with timestamp 87654321

    POST 2 with timestamp 87654321

Now the approach I have been trying to achieve is to store the posts having similar timestamps in an array. So the array would be something like

array( $timestamp => array($post1, $post2) )

By this I think I will be able to get each post besides its timestamp and then I would just display the posts attached to a timestamp.

The problem is that I cannot store multiple values in the same timestamp and I am not sure if my code has problems or what.

THE CODE:

    foreach ($my_posts as $post) {

        // Get the current post details
        $post_id    = $post->ID;
        $post_title = get_the_title( $post_id  );
        $date       = get_post_meta( $post_id, 'timeline_event_date', true );

        // Change to format of the date ( Month Date, Year)
        $parsed = date_parse_from_format('n-d-Y', $date);
        $old_date_timestamp = mktime(
                $parsed['hour'], 
                $parsed['minute'], 
                $parsed['second'], 
                $parsed['month'], 
                $parsed['day'], 
                $parsed['year']
        );
        $new_date  = date('F j, Y', $old_date_timestamp);
        $post_unix_timestamp = strtotime($new_date);

        $eventHTML[] = array( $post_unix_timestamp => array( $post ) );

    }

Can somebody help me out here please? Thanks ..

  • 写回答

1条回答 默认 最新

  • duanmorong9597 2015-06-17 21:35
    关注

    Try to change this line

    $eventHTML[] = array( $post_unix_timestamp => array( $post ) );
    

    with this line

    $eventHTML[$post_unix_timestamp][] = $post;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?