2010-03-20 11 views
5

Ich verwende die Bildanhangsseite, um Bilder, die an einen Post angehängt sind, nacheinander in einer Art Diashow anzuzeigen. Ich möchte in der Lage sein, die Gesamtanzahl der an den Elternpost angehängten Bilder und die Nummer des jeweiligen Bilds anzuzeigen, das auf einer bestimmten Anhangsseite angezeigt wird, sodass Sie das Bild und die Wörter "Image 3 of 15" sehen können beispielsweise.Wie kann ich die Anzahl der Bilder anzeigen, die einem Beitrag auf der Bildanhangsseite angehängt sind?

Update ... Ich konnte die Gesamtzahl bekommen diesen Code zu verwenden:

<?php 
    global $post; 
    $attachments = get_children(array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 
    $count = count($attachments); 
    echo $count; 
?> 

Ich kann immer noch nicht herausfinden, wie die Nummer des aktuellen Bildes zu zeigen.
Hat jemand irgendwelche Vorschläge?

Update 2 ...

Antwort des Grünschnabel- mich bekam fast da, aber es auf einmal alle Zahlen ist die Ausgabe:

„Bild 1 von 8Image 2 von 8Image 3 von 8Image 4 von 8Image 5 von 8Image 6 von 8Image 7 von 8Image 8 von 8"

Hier ist der Code I verwendet:

global $post; 
$attachments = get_children(array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 
$count = count($attachments); 
$currentImage = 1; 
foreach ($attachments as $attachment) { 
    // output your image here 
    echo "Image ". $currentImage . " of ". $count; 
    $currentImage++; 
} 

Was läuft falsch?

Update 3 - DIE ANTWORT!

global $post; 
$attachments = get_children(array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 

$count = count($attachments); 
$specific = array(); 
$i = 1; 

foreach ($attachments as $attachment) { 
    $specific[$attachment->ID] = $i; 
    ++$i; 
} 

echo "Image {$specific[$post->ID]} of {$count}"; 

Antwort

1

Dies funktioniert:

global $post; 
$attachments = get_children(array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); 

$count = count($attachments); 
$specific = array(); 
$i = 1; 

foreach ($attachments as $attachment) { 
    $specific[$attachment->ID] = $i; 
    ++$i; 
} 

echo "Image {$specific[$post->ID]} of {$count}"; 
0

hinzufügen, so etwas zu den obigen Code:

$currentImage = 1; 
foreach ($attachments as $attachment) { 
    // output your image here 
    echo "Image ". $currentImage . " of ". $count; 
    $currentImage++; 
} 
+0

Vielen Dank für die Antwort. Das hat mich fast dahin gebracht. Ich habe meine Frage oben mit dem aktuellen Problem aktualisiert. – mattz

0

Wenn Sie für ein Plugin suchen Fotogalerie zu verwalten, können Sie attachments verwenden plugin,

http://wordpress.org/plugins/attachments/

Es hält die Galerie getrennt und stellt die Bildergalerie-Shortcodes nicht in den Post-Inhalt, so dass Sie volle Kontrolle über die Bildanzeige in Ihrem Post/Seite/benutzerdefinierten Beitrag haben. Sie können auch die Reihenfolge der Bilder ändern, indem Sie einfach per Drag-and-Drop

hier ist ein Beispielcode, wie Sie Ihre Galerie Bilder abzurufen,

<?php $attachments = new Attachments('attachments'); /* pass the instance name */ ?> 
<?php if($attachments->exist()) : ?> 
    <h3>Attachments</h3> 
    <p>Total Attachments: <?php echo $attachments->total(); ?></p> 
    <ul> 
    <?php while($attachments->get()) : ?> 
     <li> 
     ID: <?php echo $attachments->id(); ?><br /> 
     Type: <?php echo $attachments->type(); ?><br /> 
     Subtype: <?php echo $attachments->subtype(); ?><br /> 
     URL: <?php echo $attachments->url(); ?><br /> 
     Image: <?php echo $attachments->image('thumbnail'); ?><br /> 
     Source: <?php echo $attachments->src('full'); ?><br /> 
     Size: <?php echo $attachments->filesize(); ?><br /> 
     Title Field: <?php echo $attachments->field('title'); ?><br /> 
     Caption Field: <?php echo $attachments->field('caption'); ?> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
<?php endif; ?> 
Verwandte Themen