Categories
fixing your desktop PHP programming

Fixing Suffusion: List posts from a single WordPress category

UPDATE: So … it seems Suffusion has (buried deep inside the config) a way of displaying category pages with a higher-level workaround to the WP “missing feature”. Although so far I can’t find a way to set the settings on individual pages (which is what you generally need). So, I’ll leave this post up – it’s a good starting point for customizing Suffusion’s page-of-pages (which is also, it seems, how you would customize its Category pages.

Just to be clear, this is not a bug in Suffision, although it’s an oversight and I hope it’ll be included in a future version. The core problem is in WordPress: people have been requesting/expecting this feature for the past 5+ years, it’s pretty basic, but for now the WP folks haven’t added it.

Problem: List all the posts in a single category

This is VERY frequently requested by WP users: you want to have a Page on your site which lists the posts that are in a single Category.

WP has a very low-level way of doing this – which is very error-prone, hard to maintain (it’s hard-coded to database ID’s!), and requires you to break every single theme you own. Every time the theme is updated, you have to manually re-implement the fix!

Fortunately, there’s a minimal workaround (which is documented in the WP docs now (scroll to bottom)) which still (!) requires some theme editing.

Fortunately, Suffusion already has most of this workaround implemented, so we can make the fix without screwing around with the theme so much. The source code in Suffusion is almost identical to the sample provided by WP – but unfortunately it’s missing a key part. In Suffusion, you CAN list posts on a Page, but you CANNOT filter those posts by category.

Solution: Combine WP’s source with Suffusion, with a bit of safety improvement

So, edit the file “posts.php”, titled (in Suffusion): “Page of Posts”.

Where it has these lines:

$args = array(
	'orderby' => 'date',
	'order' => 'DESC',
	'paged' => $paged,
);

…instead copy/paste the following:

$category_exclusive = get_post_meta($posts[0]->ID, 'category', true );

if( $category_exclusive )
{
$cat = get_cat_ID($category_exclusive);
$args = array(
	'category__in' => array( $cat ),
	'orderby' => 'date',
	'order' => 'DESC',
	'paged' => $paged,
);
}
else
{
$args = array(
	'orderby' => 'date',
	'order' => 'DESC',
	'paged' => $paged,
);
}

Usage

As per WP’s official suggestion … if you want a page to filter by category:

  1. Edit the page
  2. Select the “Page of Posts” template (this is Suffusion specific; in other themes, it might not exist)
  3. Add a “custom field” (scroll down on the edit screen), called “category”, with a value of the NAME of the category you want to be included

Explanation

One major thing here: We are being SAFE and non-destructive to the Suffusion theme.

Critically important is that we ONLY do a “filter by category” if the Page itself requests it. WP’s sample code does NOT have this protection (which is why the code above is slightly different).

This means that the rest of Suffusion (which doesn’t know about our new feature) is unaffected, and works as normal

3 replies on “Fixing Suffusion: List posts from a single WordPress category”

I like this idea as it seems simple enough, However, I keep getting the same posts (all) in every case, regardless of $cat variable getting properly set. I do see that there was an update (3.9.6) that addressed custom fields and may have broken your fix. Any thoughts would be greatly appreciated.

It seems the author has half-implemented two different competing approaches – and documented neither.

If you read the forums posts, and read every post he’s ever done, you can start to reverse-engineer the docs he should have written, and work out WTF mess he’s made of it.

Eventually, I gave up. He needs to write it properly and/or document how to use it. I’m looking for alternative themes now – too much time wasted working around his bizarre code.

It would be fine if only he’d documented it. But he didn’t – he just throws out off-hand comments in forums posts (sometimes not even correct!)

Thanks, Adam. I agree it is a mess, but I’ve gone too far down the rabbit hole – at least for now. Nice blog, BTW.

Comments are closed.