0

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.

6
  • 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, 2012 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, 2012 at 18:09
  • 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, 2012 at 23:33
  • @kaiser, my mistake; I should have been more clear about good questions and answers, re: the FAQ wordpress.stackexchange.com/faq Aug 12, 2012 at 1:00
  • @songdogtech n/p :) I enjoy having members around, that really care that much about the system as well (like you do). I just have seen some examples, where people just went through their questions in 2mins and randomly accepted stuff... which is even tough for mods to get undone. So, I hope to see you more around here and sry for the bold letters.
    – kaiser
    Aug 12, 2012 at 1:31

1 Answer 1

3

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.

6
  • I tried that but couldn't figure it out either
    – Jamie
    Aug 11, 2012 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, 2012 at 18:09
  • you want to store post-1 in $var1, post-2 in $var2 so on ??
    – Amit Kosti
    Aug 11, 2012 at 18:24
  • echo $one->post_title; this will echo the post title, echo $one->post_excerpt; will print excerpt if it has.
    – Amit Kosti
    Aug 11, 2012 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 Kosti
    Aug 11, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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