I’m working on a new theme for WordPress. It’s a generic theme that I hope will make it easier to build multiple business sites in the future. Part of the goal is integrating the Yahoo! YUI library into the superb K2 theme.
I’ve come across a problem that should be easy to solve. WordPress allows you to create a category whose posts are displayed differently than other category posts. These “asides” are short posts that appear in the side bar and not in the main body of the blog. This functionality is baked into the latest versions of WordPress and the K2 theme’s admin screen makes it really easy to use.
However, I need to add a second variation of the asides. I want to create a new landing page with three promo spots just below the topnav. This branding section would allow the site to highlight important features, promos, sales, or blog posts. This could be done with asides, however I don’t want to lose the functionality of asides in the blog section.
Asides become Promos
I’ve started by cloning the asides module and functionality and creating a new set of functions (promos). The admin screen now allows you to choose a category that will be defined as a promo. Everything seems to be working until you get to the blog post page. Blog posts labeled as the asides category appear as they should.
However, the promos category and promo posts are not following the aside functionality. I’ve looked at the loop to see where it excludes the asides category and can’t find it. I can’t find the “the_post()” function, which may be the source of the issue. I would assume that the promos module is telling the_post that “promos” category is special and these posts should not be included in the loop, nor in the category list.
help?
Have you worked with the asides functionality in WordPress? Do you have any suggestions? I’ll post a summary when I get the solution figured out.
Resources
Here’s a list of related web pages that include information but haven’t answered my questions.
- WordPress Codex: The Loop
- WP_Query function reference
- 1 post, 3 asides on front page support question
- Adding Asides
Updates
The above link for the loop has some interesting information on multiple loops within one page. I’m going through the examples for some answers. Here’s a snippet of the post:
Loop Examples
Below are two examples of using multiple loops. The key to using multiple loops is that $wp_query can only be called once. In order to get around this it is possible to re-use the query by calling rewind_posts() or by creating a new query object. This is covered in example 1. In example 2, using a variable to store the results of a query is covered. Example 3 documents the use of the update_post_caches(); function to avoid common plugin problems. Finally, ‘multiple loops in action’ brings a bunch of ideas together to document one way of using multiple loops to promote posts of a certain category on your blog’s homepage.
WordPress Codex: The Loop
Updated: the solution
During my initial modification of the files, I missed an important line that tells the loop to honor a new filter. So, if you want to duplicate the asides functionality with a new category, add this new section to wordpress/wp-content/themes/k2/app/includes/info.php
function k2promos_filter($query) {
global $k2sbm_current_module;
$promos = get_option('k2promoscategory');
// Only filter when it's in the homepage
if ( ($promos != 0) and ($query->is_home) and (!$k2sbm_current_module) and
( (function_exists('is_active_module') and is_active_module('promos_module')) or
(function_exists('is_active_widget') and is_active_widget('k2_promos_widget')) ) ) {
$priorcat = $query->get('cat');
if ( !empty($priorcat) ) {
$priorcat .= ',';
}
$query->set('cat', $priorcat . '-' . $promos);
}
return $query;
}
// Filter to remove promos from the loop
add_filter('pre_get_posts', 'k2promos_filter');
In a fit of cleverness, I changed the naming convention on my promos from promos_sidebar_module to promos_module. This threw my code off for a while.
This is the rough draft of my promos.php file that sits in wordpress/wp-content/themes/k2/app/modules/promos.php
$promos->the_post();
?>
',''); ?>
3));
register_sidebar_module_control('Promos', 'promos_module_control');
?>
There are also some changes in the options.php file and sbm section. Do a search for asides and start replacing with your new function, i.e. promos.
Remove the categories from latest posts and categories modules
The next step in this process was to make sure the promos category (and asides) don’t appear in the category and latest posts modules. These two modules sit inside the /k2/app/modules/ folder. They also use similar logic. We need to create a comma delimited list of categories to exclude from the functions that build the appropriate lists.
This code checks for aside and promos categories. It then creates a comma separator and then combines the categories into a string, i.e. “12,13”
global $k2sbm_current_module;
$promos = get_option('k2promoscategory');
$asides = get_option('k2asidescategory');
/* lets create a new variable, excludes and use this to populate the exclude=foo parameter.*/
$excludes = "";
if ( ($asides != 0) or ($promos != 0 )) {
$separator = (($asides!=0)&&($promos!=0)) ? "," : "";
$excludes = $asides . $separator . $promos;
}
For the latest posts, we’ll need to create a slightly different string. We need to create a parameter and add a negative sign to each category
$excludes = '-' . $asides . $separator . '-' . $promos;
actually, this is bad logic, I need to only add the – if the category exists. That’s what is great about blogging your code. You realize your mistakes before it is too late.
Finally, we use that excludes variable in the logic to hide the categories, for example (categories.php)
wp_list_categories('title_li=&show_count=1&hierarchical=0&exclude=' . $excludes);
These snippets assume you do not want to include your asides posts into the latest posts and categories modules. you can simplify the code if you only want to exclude the promos.
Leave a Reply