Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
the_excerpt();
endwhile;
wp_reset_query();

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
                    break;
                case 'block_two':
                                     //post two would go here and so on....
                    break;
                case 'block_three':
                    break;
                case 'block_four':
                    break;  
            }
                            }
    endif;

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. –  songdogtech 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 –  songdogtech Aug 12 '12 at 1:00
show 2 more comments

1 Answer

up vote 2 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
show 1 more comment

Your Answer

 
discard

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.