johannes.jarolim.com/yapb-forum

Full Version: Restricting thumbnail display to a single category
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Johannes,

I am using an adaptation of a function of yours named yapb_getarchives_postbypost (I found it somewhere else on this forum) to display a list of thumbnails from recent posts on my home page. (it's a custom design with no sidebar). The function by itself works well I guess.

Here's the issue - I'm dealing mainly with two categories: Sketches (Category ID 3) and Portfolio (Category ID 4). So if I just want to display thumbnails from posts of category "Sketches", I'd do something like this:

Code:
<ul class="clearfix">
<?php if (is_home() && in_category('3') ) {
    yapb_get_archives_postbypost('9', array('w=90','h=90','sx=200','sy=150','sw=400','sh=400','aoe=1','q=60','fltr[]=drop|2|2|848484|225'), '', '', false);
}
?>
</ul>

However, I'm getting thumbnails of all categories, not just category 3. and using is_category("sketches") instead of in_category('3') doesn't work either.

Another reason I'd like this filtering to work is to tweak the PHPThumb parameter values according to the category being used. For instance, I use a centered cropping for sketch thumbnails whilst I'd wish to use a top-center cropping for other categories (to display my web site portfolio, for instance - it is odd to have screen captures with the top cut off)

I have some more questions regarding the yapb_getarchives_postbypost function but for now let's keep it simple to this point. I appreciate your help/input in advance.

Beto
Hi Beto -

The original WordPress function (that i adapted a little bit here) wasn't meant to just display the posts of one category - It was meant to fetch and display all recent posts having attached an YAPB-Image so that code above won't work.

You could adapt the SQL inside the function to get all recent posts with YAPB image from one category - A quick hack would be replacing

Code:
$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);

with

Code:
$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' AND post_category = 3 ORDER BY post_date DESC" . $limit);

You could also replace the 3 with a variable and put that variable in the function header like this:

Code:
  function yapb_get_archives_postbypost_enhanced($categoryId=0, $limit='', $phpThumbParams, $before = '', $after = '', $show_post_count = false) {
    ...
    $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' AND post_category = $categoryId ORDER BY post_date DESC" . $limit);
    ...
  }

Hope that helps and
Greets from Salzburg,

Johannes
Hi Johannes,

Thank you very much for your prompt response. In theory this makes all sense and should work - but for some odd reason it just refuses to display any images with either of the methods you point up here. Which is odd, since I can see those are the corresponding category numbers and that those categories feature a number of posts that should appear.

Here's a screenshot of what I've got if it helps

[Image: wpcategories.png]

Evidently, it's categories 3 and 4 that I'd like to work with.

But if I revert to the old syntax

Code:
$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);

then all thumbnails appear again. Any idea why that happens?

Again, thank you very much for your great help.

Beto
Hi betobeto -

betobeto Wrote:But if I revert to the old syntax ... then all thumbnails appear again. Any idea why that happens?

That happens because the old version was made to do exactly that: Show all thumbnails of all categories. Beside of that i had a little mistake in my previous post since wordpress saves post2category relations in it's own table.

Here's the full (and tested) version:

PHP Code:
<?php

  
function yapb_get_category_postbypost($categoryId=0$limit=''$phpThumbParams$before ''$after ''$show_post_count false) {

    global 
$wpdb;

    if ( 
'' != $limit ) {
      
$limit = (int) $limit;
      
$limit ' LIMIT '.$limit;
    }

    
$now current_time('mysql');

    
// This is the postbypost code only

    
$query "SELECT p.* FROM $wpdb->post2cat p2c LEFT JOIN $wpdb->posts p ON p2c.post_id = p.ID WHERE p2c.category_id = 5 AND p.post_status = 'publish' ORDER BY post_date DESC" $limit;
    
$arcresults $wpdb->get_results($query);

    if ( 
$arcresults ) {
      foreach ( 
$arcresults as $arcresult ) {
        if ( 
$arcresult->post_date != '0000-00-00 00:00:00' ) {

          
// Adaption to get the YAPB-Image
          // We only output a link if there is a yapb-image present
          
if (!is_null($image YapbImage::getInstanceFromDb($arcresult->ID))) {
          
            
$post->image $image;
            
$url  get_permalink($arcresult);
            
$arc_title $arcresult->post_title;
          
            if (
$arc_title) {
              
$text strip_tags($arc_title);
            } else {
              
$text $arcresult->ID;
            }

            
// Now we insert the image html instead of the default text
            
$text '<img src="' $image->getThumbnailHref($phpThumbParams) . '" alt="' $text '" />';
            echo 
get_archives_link($url$text'html'$before$after);
          }
        }
      }
    }

  }
?>

Now we want to get the last 10 posts of your category "sketches" (ID 3) and show thumbnails of that posts that have a YAPB-Image attached:

PHP Code:
<ol>
<?
php
  yapb_get_archives_postbypost_enhanced
(310, array('w=200','q=90'), '<div class="someclass">''</div>'false)
?>
</ol> 

Voila.

Hope that helps and
Greets from Salzburg,

Johannes
Thanks Johannes.

It took me a while to figure this one out but I was finally able to come up with something that works the way I wanted. Very Happy

Anyways, for anyone else who may be interested, here's the PHP function:

PHP Code:
<?php

function yapb_get_archives_postbypost($categoryId=0$limit=''$phpThumbParams$before ''$after ''$show_post_count false) {

    global 
$wpdb;

    if ( 
'' != $limit ) {
      
$limit = (int) $limit;
      
$limit ' LIMIT '.$limit;
    }

    
$now current_time('mysql');

    
// This is the postbypost code only

    
$query "SELECT p.* FROM $wpdb->post2cat p2c LEFT JOIN $wpdb->posts p ON p2c.post_id = p.ID WHERE p2c.category_id = $categoryId AND p.post_status = 'publish' ORDER BY post_date DESC" $limit;
    
$arcresults $wpdb->get_results($query);

    if ( 
$arcresults ) {
      foreach ( 
$arcresults as $arcresult ) {
        if ( 
$arcresult->post_date != '0000-00-00 00:00:00' ) {

          
// Adaption to get the YAPB-Image
          // We only output a link if there is a yapb-image present
          
if (!is_null($image YapbImage::getInstanceFromDb($arcresult->ID))) {
          
            
$post->image $image;
            
$url  get_permalink($arcresult);
            
$arc_title $arcresult->post_title;
          
            if (
$arc_title) {
              
$text strip_tags($arc_title);
            } else {
              
$text $arcresult->ID;
            }

            
// Now we insert the image html instead of the default text
            
$text '<img src="' $image->getThumbnailHref($phpThumbParams) . '" alt="' $text '" />';
            echo 
get_archives_link($url$text'html'$before$after);
          }
        }
      }
    }

  }

?>

I preferred to include $categoryID as a variable just in case I fancy to reuse this function elsewhere.

And this is what I'm using on the HTML part of the code

PHP Code:
<ul class="clearfix">
<?
php if (is_home()) { 
yapb_get_archives_postbypost(39, array('w=90','h=90','q=60''<div class="someclass">''</div>'false);
?>
</ul> 

Works like a charm. To display another category, I just need to substitute the category ID number ("3")

PHP Code:
yapb_get_archives_postbypost(39, array('w=90','h=90','q=60''<div class="someclass">''</div>'false); 

with another number corresponding to another category ID in Wordpress. Provided it has YAPB-ized posts, of course. Cool

Guest

Hey Guys,
I've been trying to achieve about the same thing. I'm trying to use the yapb sidebar widget to display thumbnails from a specific category. I've tried both sets of code but to no avail.

I get a syntax error with one and nothing shows up with the other.

Any ideas?
Reference URL's