views:

134

answers:

2

Some background info --

In wordpress I have my portfolio as a parent page with each item of work a child page. I also have a blog.

I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.


I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.

Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).

So the final display would be a mixture of posts and child pages.

Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).

Any help would be greatful.

Thanks

A: 

I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.

Like so (default ordering is date descending):

<?php
$args = array('post_type' => 'any', 'numberposts' => 6);
$posts_and_pages = get_posts($args);
?>
vicvicvic
Thanks, that works great -- just needed to know you can have 'post_type' => 'any'Now I need to exclude specific pages and post categories from the loop. Categories are easy just 'cat' => '-19' but I can't seem to exclude pages (such as the home page).
Craig Dennis
I figured it out if($post->ID == '19') continue; ?> skips that specific post id (which is actually the page id of the homepage)Thanks again
Craig Dennis
Using continue in the loop will get you the wrong number of posts, however. If you ask for 6 posts, and then skip 2 of those in the loop, you'll only display 4. I believe you can use 'exclude' => '19' in $args for get_posts instead.
vicvicvic
+1  A: 

Use a custom query.

Example

$latest_content = new WP_Query(
    array(
        'posts_per_page'    => 6,
        'post_type'   => 'any',
        'post_status' => 'publish'
    )
);
if ( $latest_content->have_posts() )
{
    while ( $latest_content->have_posts() )
    {
        $post = $latest_content->next_post();
        // Do anything you know about the loop.
        print $post->guid . '<br>';
    }
}
rewind_posts();
toscho