• dean-dolan

    (@dean-dolan)


    Using this outside the loop to display posts from a certain category. Tried everything I can find to try and get the post featured image to be used as the background but nothing seems to work.

    Any idea how to make it?

    <?php query_posts('attachment&orderby=name&order=asc&cat=9&showposts=4'); ?>
    <?php while (have_posts()) : the_post(); ?>
    	<li style="background: url(<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>)" ><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
Viewing 1 replies (of 1 total)
  • Tirumal

    (@tirumal)

    Store the values in variables like this.

    <?php query_posts('attachment&orderby=name&order=asc&cat=9&showposts=4'); ?>
    <?php while (have_posts()) : the_post(); ?>
    	<?php
            global $post;
    	$image_url = wp_get_attachment_image_src( $attachment_id, $size, $icon );
    	$permalink = get_permalink($post->ID);
    	$title = get_the_title($post->ID);
    	?>
    <?php endwhile;?>

    and, use it outside like this.

    <li style="background: url(<?php echo $image_url; ?>)" ><a href="<?php echo $permalink; ?>"><?php echo $title; ?></a></li>

Viewing 1 replies (of 1 total)

The topic ‘Getting post image outside loop’ is closed to new replies.