简体   繁体   中英

showing an image from post in wordpress

I am working on a wordpress website. I have different category for one type of posts (call flowers for example). Suppose I have 10 posts in this category. I want to show 10 Images from these posts at home page (one image from one post )

After clicking on the image user will be redirected to the corresponding post.

Is this feasible in wordpress ? SHould i need to use custom fields or something which i found after googling.

Thanks

Setup a loop and query_posts() by the category that they are by the cat id

query_posts( 'cat=1' );

Then you could display a featured image for the post in your homepage loop, as so:

<?php if ( has_post_thumbnail() )
{
        $image_id = get_post_thumbnail_id();
        $alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
        $image_url = wp_get_attachment_image_src($image_id,'large');  
        $image_url = $image_url[0];
?>
<a href="<?php the_permalink(); ?>">
    <img src='<?php echo $image_url;?>' alt='<?php echo $alt_text; ?>' title='<?php echo $alt_text; ?>' />
</a>
        <?php
}
else
{
?>
<a href="<?php the_permalink(); ?>">
  <img src='<?php bloginfo('template_url'); ?>/images/no_image.jpg' />
</a>
<?php
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM