Harsha M V
Harsha M V

Reputation: 54949

Wordpress Displaying and Querying Posts with Featured image

I have added Featured image support for my posts by adding the following in the functions.php

add_theme_support('post-thumbnails');

I created 3 posts with Featured Image and i want to display them on the homepage as show in the Read Section in this link http://play.mink7.com/sophiance/

I am trying to do the following to get my posts.

    $args = array(
            'post_type' => 'post',
            'posts_per_page' => 3,
            'order' => 'asc'
            );

    $home_shows = new WP_Query($args);
     //   var_dump($home_shows);

    echo "<pre>";
    print_r($home_shows->posts);
echo "</pre>";

I am trying to fetch the Featured Image using the following syntax.

    $page = get_page(1);
    print_r($page);
    if ( has_post_thumbnail() ) {
       the_post_thumbnail(array(486,226));
    } 
the_content();

Now i am not sure how to call the featured image to the posts i have queried at the top. as the content is already fetched before am calling the featured image.

Upvotes: 0

Views: 3387

Answers (2)

Ravi Patel
Ravi Patel

Reputation: 5211

For perticular page get page title, content and futured image:

<?php query_posts("page_id=36");
        while ( have_posts() ) : the_post()
?>
    <h1><a href="<?php echo the_permalink(); ?>"><?php echo get_the_title();?></a></h1>

    <?php if ( has_post_thumbnail() ) { 
            the_post_thumbnail(array(486,226));
    } ?>

    <?php the_content(); ?>
<?php
    endwhile; 
    wp_reset_query();
?>      

Upvotes: 1

Ravi Patel
Ravi Patel

Reputation: 5211

Using this Query to get post title, content and futured thumbnail image:

<?php
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 3,
        'order' => 'asc'
    );
    $query = new WP_Query($args);       

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
        $query->the_post();
    ?>
      <h1><a href="<?php echo the_permalink(); ?>"><?php echo get_the_title();?></a></h1>
  <?php  
        if ( has_post_thumbnail() ) { 
            the_post_thumbnail(array(486,226));
        } 

        echo the_content();
     }
   }     

?>         

Upvotes: 2

Related Questions