WordPress Q & A: Week of July 19th, 2010

Iva asks:

For us who use shared/clustered hosting solutions, there a good and free way to run WordPress 3.0 with a couple of sub-sites using more than one database?

Check out HyperDB. It does exactly this!

Paul asks:

How can someone make sure that their theme is compatible with the latest WordPress version?

I presume Paul is asking this from a user perspective. If your theme is simple, there is only a very small chance that a WordPress upgrade will break it. This rarely happens. Plugins and advanced themes (which behave as plugins) break at a slightly higher (but still very low) rate. This has to do with how well they were coded, for the most part. If the author is using our documented public functions, it is probably fine. If they are making direct queries or using deprecated functions, it might be a problem. You can ask the author this question directly, but it would probably be faster to just throw the theme up on a fresh WordPress install (of the new WP version). For important sites, it doesn’t hurt to have a test site where you test your plugins and themes with new WP versions. That should give you more peace of mind about upgrading.

Kieron asks:

How do I change the number of posts shown in a category? Currently there are 10 but I am using 3 posts in a row so I either want to display 12 or 9 to make it look okay.

The WordPress posts per page setting is global, but plugins can alter it based on the type of page you’re viewing (or any other criterion). The Different Posts per Page plugin looks the ticket! For people looking to do this in code, try this as a base:

function my_custom_posts_per_page( &$q ) {
	if ( $q->is_category )
		$q->set( 'posts_per_page', 12 );
	/*
		You can also test things like:
		$q->is_search, $q->is_author, $q->is_year, etc
		Look in the WP_Query class for the full list of variables
		Also note that you can change the ordering!
	*/
	return $q;
}

add_filter('parse_query', 'my_custom_posts_per_page');

15 thoughts on “WordPress Q & A: Week of July 19th, 2010

  1. RE: Kieron’s question:

    I think a template hack would be better since this is specifically related to the template (he’s displaying the posts in a certain style and looking to get the correct number of posts to match that style).

    Kieron: I assume you have an archive.php, category.php, or some other template file designed specifically for your category listings. Edit that file and make the top of it look like this:

    <?php
    
    query_posts( $query_string . '&posts_per_page=12' );
    
    // the rest of the file here including get_header(), etc.

    It basically does the exact same thing as Mark’s code does (although my code may result in an extra query), but it’s a lot easier to work with IMO, especially for the novice.

    Full details on the function can be found here:

    http://codex.wordpress.org/Template_Tags/query_posts

    • Yeah, that’s another way to skin the cat. Less efficient in terms of performance (I’m guessing that’s not how you’d do it yourself), but it is simpler, and 1 line of code instead of 5.

      (You know all this, Alex, I’m just giving readers some exposition.)

    • The more I think about it, the more I like your code over mine.

      Sometimes it’s worth a little added complexity in order to gain some performance (not having to re-query the posts).

      However I would recommend your code over the plugin as it keeps theme code in the theme (functions.php) rather than making the theme rely on a plugin.

  2. Paul says:

    Mark,

    I really appreciate your input about my question. So in order words, as WP theme developer, I have to refer to the documented public functions to make sure my themes don’t break using the latest WP version.

  3. Thanks for the plugin plug, Mark!

    Do you think its functionality is something that would be good for core? If so, I would be happy to contribute it.

    (Full disclosure: MaxBlogPress originally wrote the plugin. I just don’t want to give the impression that I’m taking undue credit.)

    • Personally I’d be strongly against such functionality in the core. As Mark and your plugin has shown, there’s plenty of hooks that can be used to control the number of items per page.

      More settings is not necessarily a good thing. Best left to a plugin.🙂

  4. Is this also the best way to do it if you want to set the display for a custom post type?

    function my_custom_posts_per_page( &$q ) {
    	if ( get_post_type() == 'portfolio' )
    		$q->set( 'posts_per_page', 9 );
    	return $q;
    }
    
    add_filter('parse_query', 'my_custom_posts_per_page');
  5. Visit my site, it has alot of information about u.
    i have most top forex sites are there and top forex news is there. check it.

  6. Where did you got this much info on your blog from?? Also can i take the
    initiave to take the feeds from your blog for my yoga website?? But cant
    find the RSS feeds link here!!

    Thanks
    Md.Alamin Khan

Comments are closed.