Wordpress 在循环外显示精选帖子 [英] Wordpress display featured posts outside of loop

查看:25
本文介绍了Wordpress 在循环外显示精选帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WP 的新手,我不确定如何使用循环.我正在尝试使用以下代码在侧边栏中显示精选帖子:

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.

哦,是的,我正在使用一个可以动态调整图像大小的插件,这就是 wp_get_post_image/getphpthumburl 业务.

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

推荐答案

我不知道如何集成您的缩略图调整器,但一个开始是这个新查询,我在其中使用了多个实例(它们不会冲突)我的侧边栏显示来自特定类别的帖子.mycategory"可以是类别编号,showposts 可以是要显示的帖子数,也可以是 -1 以显示全部.

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; ?>

函数参考/WP 查询« 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); ?>

函数参考/获取帖子元 « WordPress Codex

这篇关于Wordpress 在循环外显示精选帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆