johannes.jarolim.com/yapb-forum

Full Version: Home Page - Random Thumbnail from Category List in Table (Gallery)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all

New to the forums...Great plugin, will be showcasing my new site soon I hope.

OK...I wish to pretty up my home page with a photo gallery with 'thumbnails/category name' output as a table and need help with the code.

ALL of my posts are YAPB and have images (actually the posts are just photos).

Basically my thoughts are this:

1. use wp_list_categories to get the list of categories I want
2. get a random thumbnail from the database for each of the categories in the list
3. output each instance of the list (Thumb & Cat Name) in a table.

Many thanks in advance

Rog
popcornphotography.com.au
OK...moving right along.
Working on a different approach...Watch this space
Right...Using the explode on wp_list_categories I now have a container to put the thumbnails into...BUT... am at a complete loss about where to go from here.

Here is the code as it stands: (I'll put the classes into the style sheet when I am finished)
PHP Code:
<div class="post"><h2><?php _e('Main Galleries *Test*'); ?></h2>
<p>&nbsp;</p>

<?php
    $catList 
explode('<br />'wp_list_categories('echo=0&orderby=name&hide_empty=1&depth=1&show_count=0&title_li=&style=none'));
    foreach (
$catList as $cat): ?>

<?php 
    
{
        
$iPos strpos($cat'>');
        if (
$iPos 0)
        {
            
// add the opening tag behind the opening anchor tag
            
$strLink '' substr($cat0$iPos) . '><div id="pop_gal" style="width:140px; height:160px; border:0px solid; font-size:small; float:left; text-align:center; padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px;"><div id="pop_gal_thumb" style="width:140px; height:140px; border:1px solid;">Latest thumb from this category</div><strong>' substr($cat$iPos 1) . '';
            
// add the closing tag before the closing anchor tag
             
echo str_replace('</a>','</div></strong></a>'$strLink);
         }
    } 
?>
    
    <?php endforeach ?>
    
</div> 

What I need now is a thumbnail 140x140 from each of the categories.

BTW you can see the working code here http://popcornphotography.com.au/photos/test

Help please...
Nope...Not the right approach...I think I need to ask Johannes' help on this one...

Happily generating random images now on the new site[url= http://www.popcornphotography.com.au] [/url] and still need a solution.

Code I am using is as follows:

PHP Code:
<?php 
    
global $wpdb;
    
$latest_posts $wpdb->get_results('SELECT p.* FROM ' $wpdb->posts ' p LEFT JOIN ' YAPB_TABLE_NAME ' yi ON p.ID = yi.post_id WHERE p.post_type = \'post\' AND yi.URI IS NOT NULL ORDER BY RAND() LIMIT 0,' $imagecount);
    
$thumbConfig = array('h=633','w=950','q=50','fltr[]=usm|60|0.5|3'); 
?>

Now how do we add in to only select from one category....????

Help Please
Rogerr - Sorry for the late reply, but the phrase "lots of work" seems to be the greatest understatement of the century for me right now.

I think your first approach is the better one: Start by getting all categories (incl. category id) outside the loop. Then cycle through the categories and create individual wordpress loops - each selecting all posts of that category with a limit of 1. Then cycle through and display thumb+text.

Don't have time for a code sample right now, but i'll look at that tomorrow.

Sorry again from Salzburg,

Johannes
(11-27-2008 08:47 PM)Johannes Wrote: [ -> ]Rogerr - Sorry for the late reply, but the phrase "lots of work" seems to be the greatest understatement of the century for me right now.

I think your first approach is the better one: Start by getting all categories (incl. category id) outside the loop. Then cycle through the categories and create individual wordpress loops - each selecting all posts of that category with a limit of 1. Then cycle through and display thumb+text.

You are correct...Sorry, I forgot to post my solution to this problem...Here goes.

PHP Code:
<?php query_posts('category_id=474,476,478,482,484,486,488,490,493&showposts=999');?>

<?php $posts get_posts('category=474,476,478,482,484,486,488,490,493&numberposts=20&orderby=rand');
    foreach (
$posts as $post) : start_wp(); ?>
    
<li><a title="<?php echo $current_post->post_title ?>" href="<?php echo get_permalink($current_post->ID?>"><?php if (yapb_is_photoblog_post()): ?>  
  <?php
    
echo yapb_get_thumbnail(
      
''// HTML before image tag
      
array(
        
'alt' => ''// image tag alt attribute
        
'rel' => 'lightbox'                      // image tag rel attribute
      
),
      
'',               // HTML after image tag
      
array('w=900''h=633''q=50','fltr[]=usm|80|0.5|3'), // phpThumb configuration parameters
      
''             // image tag custom css class
    
);
  
?></a></li>

<?php else: ?>
<li>Loading...</li>
<?php endif ?>

<?php endforeach; ?>

This is a bit clunky as I have to remember to change the code manually every time I add a category that I want to select from but at least it does the job.

If you can think of a better way...especially for the following I would be very appreciative.

<?php query_posts('category_id= "[I would like to just include child_of=1]" here instead of adding all the children manually.

Again, thanks for all your hard work on this brilliant plugin.
Hi rogerr -

I'd implement your first requirement

Quote:1. use wp_list_categories to get the list of categories I want
2. get a random thumbnail from the database for each of the categories in the list
3. output each instance of the list (Thumb & Cat Name) in a table.

the following way:

PHP Code:
<?php

  
// Attention: Whole code has to be placed outside the WP LOOP

  // single point for changes

  
$category_ids = array(474,476,478,482,484,486,488,490,493);

  echo 
'<ul>';

  foreach (
$category_ids as $category_id) {

    
// Get one random post out of the current category
    // Attention: There is no code whatsoever to ensure we get a YAPB post

    
query_posts('cat=' $category_id '&showposts=1&orderby=rand');

    
// Let's start the current WP LOOP (With one post)

    
if (have_posts()) {
      while (
have_posts()) {
        
the_post();

        echo 
'<li>';

        
// Now the check for YAPB
        // Always ask for existance of the template function:
        // Your blog will work even if you disable YAPB
        // (while upgrading for example)

        
if (function_exists('yapb_is_photoblog_post') && yapb_is_photoblog_post()) {
          
// Output image +  text
        
} else {
          
// Output text
        
}

        echo 
'</li>';

      }

    }

  }

  echo 
'</ul>';

?>

Advantage: Code is simple and readable
Disadvantage: 9 additional SQL fetches

P.S.: I really wouldn't use tables for that Wink

Greets from Salzburg,

Johannes
Reference URL's