Wordpress display featured posts outside of loop

2019-08-25 09:24发布

Im new to WP and Im not real sure how to work with the loop. I am trying to display featured posts in the sidebar with this code:

<?php
query_posts('cat=5');
$url = get_permalink();
while(have_posts()){
the_post();
$image_tag = wp_get_post_image('return_html=true');
$resized_img = getphpthumburl($image_tag,'h=168&w=168&zc=1'); 

$title = $post->post_title;

echo "<ul class='left_featured'>";
echo "<li><a href='";
echo $url;
echo "'><img src='$resized_img' width='168' height='168' ";
echo "'/></a></li>";
echo "<li><a href='";
echo $url;
echo "'/>";
echo $title;
echo "</a></li></ul>";
echo "";
};
?>

This gives me all sorts of crazy outputs, text from random posts, images, etc...Its supposed to output a list of images and titles for all of the posts in a certain category. Any help would be really appreciated.

Oh yeah, I am using a plugin that resizes images on the fly, thats what the the wp_get_post_image/getphpthumburl business is.

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-25 10:09

I don't know how to integrate your thumbnail resizer, but a start is this new query, which I use multiple instances (they won't conflict) in my sidebar to show posts from a particular category. "mycategory" can be a category number and showposts can be a number of posts to show or -1 to show all.

<?php $my_query = new WP_Query('category_name=mycategory&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="Permanent Link to: 
<?php the_title_attribute(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>

Function Reference/WP Query « WordPress Codex

You could add image info in a custom field and then call that:

<?php echo get_post_meta($post->ID, "image", $single = true); ?>

Function Reference/get post meta « WordPress Codex

查看更多
登录 后发表回答