I am trying to use 3 custom fields from wordpress posts to build an array. I need to show and hide a div based on what time it is. I have a start, an end and a name which is also set as a css class in the template. That part works if I set the array manually. But I want to build it on the fly. Not having too much luck. Can someone help?

$hrs = array(
        array("2013-07-27 13:00", "2013-07-27 13:49", "cucu"),
    );


//get current category id
    $current_cat = get_query_var('cat');

    //get posts that belong to current category
    $args = array( 'category' => $current_cat, 'post_type' =>  'post' );
    $postslist = get_posts( $args );

    $hrs = array();

        foreach ($postslist as $post) :  

        $key_1 = get_post_meta( $post->ID, 'Start',true );
        $key_2 = get_post_meta( $post->ID, 'End',true );
        $key_3 = get_post_meta( $post->ID, 'Name',true );

    $hrs[]=$key_1;
    $hrs[]=$key_2;
    $hrs[]=$key_3;

    endforeach;

    var_dump($hrs);

That loop will generate:

$hrs = array("2013-07-27 13:00", "2013-07-27 13:49", "cucu");

If you need a multidimensional array an easy solution is to start like this:

$result = array();
$hrs = array();

And after the loop:

$result[] = $hrs;
var_dump($result);

If you don't have other segments to add, i.e.:

$result = array(
    array("2013-07-27 13:00", "2013-07-27 13:49", "cucu"),
    array("2013-07-27 15:00", "2013-07-27 15:49", "hello"),
    array("2013-07-27 17:00", "2013-07-27 17:49", "abc"),
    );

you can probably use your current script and change the part of it that will use this output.

Hi,

This is the test link: http://jobs.smoige.com/category/cat1/

So it should be like this? Cause I don't get it ...

  //get current category id
    $current_cat = get_query_var('cat');
    //get posts that belong to current category
    $args = array( 'category' => $current_cat, 'post_type' =>  'post' );
    $postslist = get_posts( $args );
    $hrs = array();
        foreach ($postslist as $post) :  
        $key_1 = get_post_meta( $post->ID, 'Start',true );
        $key_2 = get_post_meta( $post->ID, 'End',true );
        $key_3 = get_post_meta( $post->ID, 'Name',true );

   $result = array();
   $hrs = array();

    endforeach;

    $result[] = $hrs;
    var_dump($result);

Outside and before the foreach loop, otherwise both are restarted after each iteration, so:

$result = array();
$hrs = array();

foreach($postslist as $post)
{
    $hrs[] = get_post_meta( $post->ID, 'Start',true );
    $hrs[] = get_post_meta( $post->ID, 'End',true );
    $hrs[] = get_post_meta( $post->ID, 'Name',true );
}

$result[] = $hrs;
var_dump($result);

Thanks bro... but it still doesn't work right.

i need an array like this ...

$hrs = array(
        array("2013-07-27 13:00", "2013-07-27 13:49", "cucu"),
        array("2013-07-27 13:50", "2013-07-27 14:00", "bau"),
        array("2013-07-27 14:00", "2013-07-28 15:00", "lala"),
    );

If I manually set the start and end time and it includes the time it is right now, I get the name in an array and add a class to a div with jquery. but now it s not doing it and i ve changed the custom field value.

Ok my previous suggestion was wrong, excuse me, this should solve your issue:

$hrs = array();
$i = 0;

foreach($postslist as $post)
{
    $hrs[$i][] = get_post_meta( $post->ID, 'Start',true );
    $hrs[$i][] = get_post_meta( $post->ID, 'End',true );
    $hrs[$i][] = get_post_meta( $post->ID, 'Name',true );
    $i++;
}

var_dump($hrs);

Otherwise on previous version you can use array_chunk():

print_r(array_chunk($hrs, 3));

Yeah ... that worked. Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.