Support » Themes and Templates » Calling One Post Without a Loop?

  • I have a question which I am sure has been asked several times, but I cannot find this on Google.

    I have a loop using WP_Query, but I know I can also use get_posts. My code below uses $posts->have_posts() for the loop, but I don’t think I should have to use a loop to get just one post.

    How can I add the below code without the $posts->have_posts() loop in my theme? is there an array I can use instead like $posts[0]?

    $posts = new WP_Query(array(
       'post_type' => 'home_page_highlights',
       'posts_per_page' => 1,
       'meta_key' => 'sort',
       'meta_value' => $sort,
    while ( $posts->have_posts() ) : $posts->the_post();
        echo "<h1>" . get_the_title() . "</h1>";
        echo "<p>" . the_content() . "</p>";

    [please mark any posted code – ]

    [please do not bump]

  • The topic ‘Calling One Post Without a Loop?’ is closed to new replies.