2009-08-21 118 views
1

我有以下代码从我用来显示在存档页面的帖子中拉出自动生成的缩略图图像。该代码在我的本地服务器上正常工作,但只要我将它上传到网络,它就无法工作。WordPress自动生成的缩略图问题

----编辑-----

什么它现在显示是每一个岗位相同的缩略图,一个链接到的第一篇文章进入。任何想法,为什么这可能是?

<ul> 

<?php query_posts('cat='.get_query_var('cat').'&order=ASC'); ?> 

    <?php if (have_posts()) : ?> 

     <?php while (have_posts()) : the_post(); ?> 

     <?php 
//Get images attached to the post 

$args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'numberposts' => -1, 
     'order' => 'DESC', 
    'post_status' => null, 
    'post_parent' => $post->ID 
); 
$attachments = get_posts($args); 
if ($attachments) { 
    foreach ($attachments as $attachment) { 
     $img = wp_get_attachment_thumb_url($attachment->ID); 
       break; 
     } 
} 
?> 

      <li> 
       <img src="<?php echo $img; ?>" alt="" /> 
       <h2 class="small"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
      </li>     

     <?php endwhile; ?> 

     <?php endif;?> 

     </ul> 

回答

2

回应您的编辑。你需要确保在每次迭代while()循环后重置$ img。然后,您需要在写入图像标签之前进行检查以确定其设置。这将停止重复相同的缩略图。示例代码如下。

现在它重复着,因为它为第一篇文章找到了一张图片,而对其他人却没有。但是在第一篇文章中设置了$ img,所以它继续用于所有其他文件,因为它永远不会被重置或更改。

<ul> 

<?php query_posts('cat='.get_query_var('cat').'&order=ASC'); ?> 

    <?php if (have_posts()) : ?> 

     <?php while (have_posts()) : the_post(); ?> 

     <?php 
//Get images attached to the post 
$img = false; 
$args = array(
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'numberposts' => -1, 
     'order' => 'DESC', 
    'post_status' => null, 
    'post_parent' => $post->ID 
); 
$attachments = get_posts($args); 
if ($attachments) { 
    foreach ($attachments as $attachment) { 
     $img = wp_get_attachment_thumb_url($attachment->ID); 
       break; 
     } 
} 
?> 

      <li> 
       <?php if ($img): ?><img src="<?php echo $img; ?>" alt="" /><?php endif; ?> 
       <h2 class="small"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
      </li>     

     <?php endwhile; ?> 

     <?php endif;?> 

     </ul> 
+0

不幸,这也没有工作 - 虽然它非常有意义,应该工作。 hmmmm ............ – DanC 2009-08-21 14:16:08

0

也许它缺少服务器上的GD库?你检查过phpinfo()来验证吗?

+0

它运行起来,快速检查后 – DanC 2009-08-21 13:21:59