【问题标题】:WordPress auto generated thumbnails issueWordPress自动生成的缩略图问题
【发布时间】:2009-08-21 13:12:08
【问题描述】:

我有以下代码从我用来显示在存档页面的帖子中提取自动生成的缩略图图像。该代码在我的本地服务器上运行良好,但一旦我将其上传到网络,它就无法正常工作。

----编辑-----

现在显示的是每个帖子的相同缩略图,链接到输入的第一个帖子。任何想法为什么会这样?

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

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    响应您的编辑。您需要确保在 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>
    

    【讨论】:

    • 不幸的是,这也不起作用-尽管它非常有意义并且应该起作用。嗯…………
    【解决方案2】:

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

    【讨论】:

    • 它已经启动并运行,经过快速检查
    猜你喜欢
    • 2019-04-10
    • 2011-08-07
    • 2019-02-14
    • 2013-09-11
    • 2011-09-04
    • 2011-02-08
    • 2015-07-19
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多