WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on a site. This site has a built in layout organize that uses a switch statement to organize data on a page. I want to use that to organize the homepage posts. Right now, there are 4 posts that I have a loop set up to return. Here is the loop I am running

query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => -1 ) );
global $more;
$more = 0;
while ( have_posts() ) : the_post();

I need to be able to get the 4 posts returned into this switch

$layout = $data['homepage_layout']['enabled'];

    if ( $layout ) :
        foreach ( $layout as $key => $value ) {
            switch ( $key ) {
                case 'block_one' :
                                     //post one would go here
                case 'block_two':
                                     //post two would go here and so on....
                case 'block_three':
                case 'block_four':

For the life of me, I cannot figure it out.

share|improve this question
Please work on your older questions. With most you didn't even comment and people invested a lot of time to help you. Thanks. – kaiser Aug 11 '12 at 13:12
I don't know what you mean. I am still new to this site and I have only asked five questions. Do you mean that I need to accept answers bu clicking the checkmark? – Jamie Aug 11 '12 at 18:09
Jamie, yes, accept answers; and also uptick them, if they are esp. helpful. – markratledge Aug 11 '12 at 19:03
what is the uptick thing suppose to do? The system won't allow me to do that anyway. It says I don't have enough reputation. not sure why you need it but I am sure there must be a reason. I am figuring it out. Slowly – Jamie Aug 11 '12 at 23:33
@kaiser, my mistake; I should have been more clear about good questions and answers, re: the FAQ wordpress.stackexchange.com/faq – markratledge Aug 12 '12 at 1:00
up vote 3 down vote accepted

Use get_posts instead -

You can use the function get_posts() to get all the posts as array. This function accepts almost all parameters that of query post's.

Example -

    $args = array( 'numberposts' => 3,'category' => 3 );
    $homePost = get_posts( $args );
    $one = $homePost[0]; 
    $two = $homePost[1]; 
    $three = $homePost[2];

    //print_r($one);        // lets see what we have in array - $one

    //e.g. to print the title
    echo $one->post_title; 

Note -

In this case you'll not be able to use typical Wordpress function's like the_title(), the_excerpt() etc. You have to manually echo each element of array.

Tip - Do a quick print_r(); to see what else you have in array and use it.

share|improve this answer
I tried that but couldn't figure it out either – Jamie Aug 11 '12 at 6:53
This is what I was trying to figure out $args = array( 'numberposts' => 3,'category' => 3 ); $homePost = get_posts( $args ); //print_r($homePost[0]); echo $homePosts[0]->the_excerpt; $one = $homePost[0]; $two = $homePost[1]; $three = $homePost[2]; – Jamie Aug 11 '12 at 18:09
you want to store post-1 in $var1, post-2 in $var2 so on ?? – amit Aug 11 '12 at 18:24
echo $one->post_title; this will echo the post title, echo $one->post_excerpt; will print excerpt if it has. – amit Aug 11 '12 at 18:37
Note - to show excerpt - echo $var->post_excerpt;, and this will print only if you manually added text to excerpt field in post edit page. the_excerpt() function won't be here to calculate excerpt for you. – amit Aug 11 '12 at 18:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.