phpwordpresswordpress-theming

List most recent posts and pages


I'm making my own template for my portfolio. This is my first from scratch up theme in Wordpress so bear over with me, if I don't talk the lingo or know what things are called... ;-)

I've made a frontpage where I want to loop through the last posts in category "Featured" and the last pages with parent page "Cases", limited with 5.

I know how to loop through a list of category posts, but how do I combine that with latest pages?


Solution

  • Here is a loop to get posts that are a child of:

    $args = array(
        'post_type' => 'page',
        'numberposts' => 5,
        'post_status' => 'publish',
        'post_parent' => 33, // change this to the ID of the page you need
    );
    
    $posts = get_posts($args);
    
    if ($posts) {
        foreach ($posts as $post) { 
             setup_postdata($post);
            // Your PHP code here.
        }
    }
    

    All of the Wordpress post getting functions will return the posts in an array. If you want to "combine" them you can either do an array_merge and put the posts from the category loop and the page loop in the same array and iterate through them, or you can do multiple foreach or while loops.