• Hi All,

    I added the following call to my functions.php and have now lost the thumbnails on my home page.

    Any suggestions? If I comment out the call to add_filter then they show up again.

    # Pre-fetch for posts
    function setcat_pre_posts($query) {
    	if ($query->is_home) // Assuming you want this to apply when it's your home page
    	{
    	  $domain = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
    	  $domainstr = substr($domain, 0, -4); 
    
              $sitecat_id = get_category_by_slug($domainstr)->term_id;
              $generalcat_id = get_category_by_slug('general')->term_id;
              $cats = $sitecat_id.','.$generalcat_id;
              $query->set('cat', $cats);
    	}
    	return $query;
    }
    
    add_filter('pre_get_posts', 'setcat_pre_posts');

    I should probably post this as well. Here’s the code being used in functions.php to get the thumbnail.

    # Displays post image attachment (sizes: thumbnail, medium, full)
    function dp_attachment_image($postid=0, $size='thumbnail', $attributes='') {
    	if ($postid<1) $postid = get_the_ID();
    	if ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'post_mime_type' => 'image',)))
    		foreach($images as $image) {
    			$attachment=wp_get_attachment_image_src($image->ID, $size);
    			?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
    		}
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • What exactly are you doing here?

    $domain = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
    	  $domainstr = substr($domain, 0, -4); 
    
              $sitecat_id = get_category_by_slug($domainstr)->term_id;
              $generalcat_id = get_category_by_slug('general')->term_id;

    If you use the IDs outright as i posted in the other thread, does the problem still occur?

    Thread Starter monkeymynd

    (@monkeymynd)

    I am getting the category based on the domain name. Yes, if I put the actual ID’s the problem still remains.

    Actually, I figured out the problem this morning, but you may have a cleaner way of fixing it.

    Since the filter is being run on the posts, it is filtering out the post attachments. I took a look in the database and it seems that attachments default to category ‘1’. So, let’s say a post is in category 3…well, the attachment for that is in category 1, so it gets filtered out and doesn’t show up.

    I just added category 1 into the line that sets the categories and they now show up. It works for us because we don’t have any uncategorized (cat 1) posts, but if one were to be in that category it would make it on to a page when it shouldn’t be there.

    Thanks for the reply btw!

    Ok i understand the problem but a little unsure how to fix (i’ll have a think about it)..

    RE: the domain bit
    Why go through those hoops when you could just type the name in though….

    Looks to me like you’re taking the domain name (i’ll use google as an example) and using what’s left as the category name..

    ex:

    http://www.google.com
    or
    www.somesite.com

    becomes..

    google
    or
    somesite

    Why though? … i can’t see how this helps..

    If you know the category names, then just use the name …

    $someid = get_cat_ID('CATNAME');

    I’ll have a ponder regarding attachments..

    I’ve tested with the function you posted for attachments and without, and also tested with and without the pre_get_posts filter…

    Attachments work in all cases..

    Do you have any parameters in the query_posts line for whatever file handles that page (index.php perhaps) ?..

    Thread Starter monkeymynd

    (@monkeymynd)

    We have a unique need for our sites. Basically, we have sites that are being used to service different areas. Ex. http://www.area1.com, http://www.area2.com, http://www.area3.com, etc.

    What we’re doing, is using one database to service all of the sites. The site admin for all sites is done from only one of the sites. We don’t have each different site stored in different tables within the database. We have only one set of tables for all sites.

    Then, we use the url to determine what info the calling site is able to see. For instance, if we’re creating a general post, then we put it in the General category and it is seen on all sites. Else, if we want area1 & area2 to see something, but not area3, then we tag it in category Area1, Area2.

    This is actually working great for us, but we have had to hack up the template code just a bit.

    Without seeing the code you have (and i’d imagine that’s several pages worth of customization) it’s hard to say… (i’m not asking you to post the code)….

    Perhaps try using the category__in paramter instead (you’d need to array the IDs with category__in), as you could be using cat in your template files…

    I think what’s happening is that the filter is saying …
    cat=4,5

    Then in one of your files you’re declaring a cat, ie. query_posts('cat=6

    Best guess really, it could be any number of things…

    The easiest way (i can see) to debug the problem would be to run a local installation with your customizations and systematically remove chunks and see when things start to work correctly with the filter…

    I can only go so far in testing the code without a direct copy of your files and customizations (again not asking for them)..

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Thumbnails missing after add_filter’ is closed to new replies.