[Wordpress] Willekeurige thumbnail van specifieke categorie

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • TheNephilim
  • Registratie: September 2005
  • Laatst online: 17-09 11:07
Oké een klant wil graag 3 plaatjes op elke pagina die willekeurig gekozen worden. De plaatjes moeten afkomstig zijn van een op te geven categorie, anders krijg je ook thumbs van bijlages die niet intressant zijn op die plek. Het fotoalbum zit zo in elkaar Categorie: Fotoalbum -> Bericht: Album -> Bijlages: foto's

Nou heb ik al even lopen zoeken en klooien, maar ik krijg het niet voor elkaar. Zoals het er nu uit ziet kan het gewoon niet.

http://wordpress.org/supp...il-from-specific-category en http://php.quicoto.com/ho...st-attachments-wordpress/ geven allebei wel thumbs terug, maar niet uit de juiste categorieën.

Heeft iemand hier een idee hoe ik dit op kan lossen?

In eerste instantie had ik dit:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
query_posts("cat=".$instance['in_category']."&posts_per_page=".$instance['items_to_show']."&orderby=rand");
        if (have_posts()) : while (have_posts()) : the_post();
        
            
            $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'teaser-220x138');
            echo "<div style=\"float: left; width: 220px; height: 138px; margin-right: 20px;\">
                      <img src=\"".$thumbnail[0]."\" />
                      <div style=\"position: absolute; width: 103px; height: 19px; margin-top: -26px; padding-top: 3px; padding-left: 5px; background: url(".get_stylesheet_directory_uri()."/images/teaser_label.png);\">
                          <a href=\"".get_permalink()."\">Bekijken...</a>
                      </div>
                  </div>";
        
        endwhile; endif;


Maar dit laat alleen de uitgelichte afbeelding zien en dus niet een willekeurige afbeelding uit de goede categorie.

M'n laatste poging:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
query_posts("cat=".$instance['in_category']."&posts_per_page=".$instance['items_to_show']."&orderby=rand");

        if ( have_posts() ) : while ( have_posts() ) : the_post();


            $thumb = get_post_meta(get_the_ID(), 'thumb', $single = true);


            if($thumb == "") {
                $attachments = get_children( array(
                    'post_parent' => $post->ID,
                    'numberposts' => 1,
                    'post_type' => 'attachment',
                    'orderby' => 'rand',
                    'post_mime_type' => 'image')
                    );            

                 foreach ( $attachments as $att_id => $attachment ) {


                    $thumbnail = wp_get_attachment_image_src($att_id, 'teaser-220x138', true);
                    
                    echo "<div style=\"float: left; width: 220px; height: 138px; margin-right: 20px;\">
                              <img src=\"".$thumbnail[0]."\" />
                          </div>";
                    
                }

            }

        endwhile;
        endif;

[ Voor 23% gewijzigd door TheNephilim op 25-10-2011 15:24 ]


Acties:
  • 0 Henk 'm!

  • TheNephilim
  • Registratie: September 2005
  • Laatst online: 17-09 11:07
Oplossing! Misschien niet zo netjes, maar je moet soms wat...

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$pictures = array();
        
        query_posts("cat=".$instance['in_category']."&posts_per_page=".$instance['items_to_show']."&orderby=rand");
        if (have_posts()) : while (have_posts()) : the_post();
        
        
            $args = array(
                'post_type' => 'attachment',
                'numberposts' => null,
                'post_status' => null,
                'post_parent' => get_the_ID()
            );
            $attachments = get_posts($args);
            if ($attachments) {
                foreach ($attachments as $attachment) {
                    $thumbnail = wp_get_attachment_image_src($attachment->ID, 'teaser-220x138');
                    $pictures[] = $thumbnail[0];
                }
            }
        
        endwhile; endif;
        
        shuffle($pictures);
        
        $i=1;
        
        foreach ($pictures AS $pic) {
            echo "<div style=\"float: left; width: 227px; height: 144px; margin-right: 10px; border: 1px solid #c0c0c0;\">
                      <img src=\"".$pic."\" style=\"border: 3px solid #f0f0f0;\" />
                  </div>";
            if ($i == 3) break;
            $i++;
        }


Ik haal gewoon alle pics op, gooi ze in een array en ga er dan 3 uitkiezen...