Frage

Ich möchte meine Kategorienliste (archive.php) anpassen, damit es ein Miniaturbild des ersten Bildes zeigt, das jedem Beitrag angehängt ist

Anscheinend ist die Archive.php -Datei jedoch eine derjenigen, die das Anhangsobjekt nicht nativ unterstützen. Zum Beispiel wird der folgende Code das meiste tun, was ich möchte (obwohl ich, wenn kein Anhang gefunden wird, ein leeres Bild erhalte, muss ich das beheben).

Ich befürchte jedoch, dass eine Auswahl in einer Schleife wie diese für das, was ich versuche, vielleicht viel zu teuer ist.

Irgendwelche Ideen?

    <?php while (have_posts()) : the_post(); ?>
    <?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?>
        <div class="searchItem" style="clear:both;">
            <h3 id="post-<?php the_ID(); ?>"><img src="<?php echo wp_get_attachment_url($attachment_id); ?>" class="post-attachment" /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('l, F jS, Y') ?></small>
            <div class="excerpt"><?php echo $post->post_excerpt; ?></div>
            <div class="postmetadata">Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
        </div>
    <?php endwhile; ?>
War es hilfreich?

Lösung

Sie können die WordPress -Funktion get_children verwenden. Obwohl ich nicht denke, dass es einen Unterschied macht, leistungsfähig.

<?php while (have_posts()) : the_post(); ?>
    <?php $attachment = array_values( get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'numberposts'  => 1 ) ) ); ?>
    <div class="searchItem" style="clear:both;">
        <h3 id="post-<?php the_ID(); ?>">
        <?php if( $attachment ) echo '<img src="' . wp_get_attachment_url($attachment[0]->ID) . '" class="post-attachment" />'; ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        <small><?php the_time('l, F jS, Y') ?></small>
        <div class="excerpt"><?php echo $post->post_excerpt; ?></div>
        <div class="postmetadata">Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
    </div>
<?php endwhile; ?>

Andere Tipps

WP hat eine Kernfunktion, um dies anzuzeigen, siehe meinen Beitrag http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top