This is a “custom” loop outside of the main WordPress query (query_posts
), you will have to tell WordPress to setup the post data using setup_postdata()
More info on get_posts()
is found here, giving you basically what I am about to write below: http://codex.wordpress.org/Template_Tags/get_posts
Tip: The WordPress Codex is the best friend you will ever have, apart from Google.
<?php
$the_slug = 'my-page';
$args = array(
'name' => $the_slug,
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) {
echo 'ID on the first post found '.$my_posts[0]->ID;
// To get the content of the first post:
echo apply_filters('the_content', $my_posts[0]->post_content);
// if you now wanted to remove the first post from this loop and assign it to a different variable $first_post
// However, it looks as if you are only grabbing one "post" being a "page" from the slug "my-page"
$first_post = $my_posts[0];
unset($my_posts[0]);
foreach($my_posts as $p): setup_postdata($p);
// Now you can use the_title(), the_content() etc as you normally would
endforeach;
}
// Reset WordPress Loop & WP_Query
wp_reset_postdata();
?>