1

アーカイブページに表示するために使用する投稿から自動生成されたサムネイル画像を取得する次のコードがあります。コードはローカル サーバーでは正常に動作しますが、Web にアップロードするとすぐに動作しません。

- - 編集 - - -

現在表示されているのは、最初に入力された投稿にリンクされた、すべての投稿の同じサムネイルです。なぜこれが考えられるのでしょうか?

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

2 に答える 2

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>
于 2009-08-21T13:32:54.370 に答える
0

サーバー上の GD ライブラリが見つからない可能性がありますか? phpinfo() を確認して確認しましたか?

于 2009-08-21T13:15:07.123 に答える