WordPress.org

Ready to get started?Download WordPress

Forums

Calling One Post Without a Loop? (1 post)

  1. hotwebideas
    Member
    Posted 1 year ago #

    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>";
    endwhile;

    [please mark any posted code - http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    [please do not bump]

Topic Closed

This topic has been closed to new replies.

About this Topic