在Wordpress中获取帖子的图像

时间:2012-12-23 20:40:12

标签: wordpress wordpress-theming

我想在wordpress中制作幻灯片,我为它制作了一个类别,现在我想为我的幻灯片放映该类别的图像。这是我的代码的一部分:

<div id="slide-show">
   <ul>
        <?php query_posts('cat=1'); while(have_posts()): the_post();?>
        <li>
            <a href="<?php the_permalink();?>">
            <img src="<?php
            $image=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
            echo $image[0];?>" alt="<?php the_title();?>" /></a>
        </li>
        <?php endwhile; wp_reset_query();?>
    </ul>
</div>

但它不起作用。请有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

首先,我repeat自己:

  

首先,不要使用 query_posts   请检查:

     

在这种情况下,我建议构建一个函数来创建所需的输出。您将其放在主题functions.php文件中。把它放在文件的末尾。
如果有任何?>作为PHP文件的最后一项,将其删除,则没有必要,如果之后有任何空格,可能会破坏您的网站

也就是说,我们的函数将打印出您想要的Html,并在任何主题模板文件(index.phpsingle.php,{{1}中调用它等等)。

page.php

数字4是类别ID。

这是函数,检查代码中的注释:

<?php get_gallery(4); ?>

<强>结果:
html output screenshot