这是WordPress主查询之外的“自定义”循环(query_posts
), 您必须告诉WordPress使用setup_postdata()
有关的详细信息get_posts()
在这里可以找到,基本上给你我下面要写的内容:http://codex.wordpress.org/Template_Tags/get_posts
提示:除了谷歌,WordPress Codex是你最好的朋友。
<?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();
?>