Improve your WordPress site by forcing plugins to put JS at the bottom of the page

WordPress makes it easy to add features by installing plugins. While most of these are well designed, they often use WordPress defaults that are not the greatest for accessibility or performance. One of the key elements of making your site load faster is to put the JavaScript at the bottom of the page, just before closing the body tag.
Continue Reading Improve your WordPress site by forcing plugins to put JS at the bottom of the page

Multiple asides categories in WordPress?

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.

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

have_posts() ):
$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.

Creating a static home page and dynamic blog page in WordPress 2.3

I’m having issues right now with WordPress. The problem isn’t lack of documentation, it’s the lack of current and consistent information that is driving me crazy.

WordPress 2.2x introduced functionality to choose a page as the static home page. This feature fixed the need for several plugins and hacks.

  1. Create a page in your WordPress Admin with your preferred home page info
  2. Go to the options –> Reading submenu and select “Front page displays:”. For the front page, choose the page you just created
  3. Check your tabs, do you have duplicate home page tabs? I had a custom topnav and had to add logic to not display the new page in the top nav
  4. You may want to re-arrange the tabs to put your new home page in first place. Here’s a great plugin: mypageorder.

Static home page is complete

So, that was fairly easy. You now have a static home page, but those great blog posts you’ve been writing are missing. The static home page does not activate the Loop, the code that handles blog posts, archives, etc. So now you need to create a new section for your blog posts.

This is where it gets confusing. There are as many suggestions as conflicts. For every version, there is a reference to conflicts with this page, this version, this etc… The following are some of the twiki suggestions

Setting up your blog in a different subroot

This twiki section describes how to set up your blog post outside the root. However, notice the comments toward the end about conflicts. I’ve tried this method and couldn’t get it to work in WordPress 2.3.1.

Suppose you have WordPress running at http://example.com/. Suppose further that you want your blog to be located at http://example.com/blog/ and that you want other pages to be available at http://example.com/page1/.

The first thing you will want to do is to create a home page. You can do this by creating home.php in your theme directory. Next, create a blog template. The easiest way to do this is to create a file named blog.php with the following contents in your theme directory:



Log into WordPress and create a page named “Blog” with template “blog”. You’re done. The one last thing you will want to do is to update your permalinks structure to begin with “/blog/”, ie, “/blog/%year%/%monthnum%/%postname%/”. Now you have a WordPress-managed CMS with a unique front page and logically-structured blog content.

If you’re using the <!– more –> quicktag to place things below the fold in your posts, you’ll want to include the global variable $more and set it to zero (as shown in the code above). Otherwise, the entire post will print out on your blog’s main page.

As noted above, the home.php file is not needed in versions 2.1+. If both the blog.php and home.php files are deployed in 2.1+ they will conflict and cause the blog.php templated page to appear blank. [emphasis – TED]

Making your blog appear in a non-root folder

The outdated resource page

WordPress has a page in its twiki specifically for static pages: Creating a Static Home Page. However, it’s filled with warnings about conflicts and doesn’t solve the lack of blog pages.

UPDATE: With the release of WordPress Version 2.1, the option to set your own front page can be accomplished via your Administration > Options > Reading panel. However unlike the old explanation that follows, you must NOT name your home page template file “home.php”. If you do, this will cause a conflict with the WordPress 2.1 system. The approach that follows on this page is no longer necessary with WordPress 2.1 or newer
Creating a Static Home Page

Reading Options Subpanel

The Reading Options Subpanel twiki tells you how to select a page to be your new blog index. It just doesn’t tell you how to create that page.

…A static page (select below) – select this option to cause a “static” Page to be displayed as your blog’s front page. At the same time, choose the Page that will display your actual Posts. The Front page and Posts page cannot be the same value.

  • Front page – in the pull down box, select the actual Page that you want displayed as your front page. If you do not select a choice here, then effectively your blog will show your posts on both the blog’s front page and on the Posts page you specify. If you would like to create a static home page template file, do not name it home.php, otherwise you will encounter problems when you try to view the “blog”/”posts” section of your site. To get around this, just name it anything else, example: myhome.php
  • Posts page – in the pull down box, select the name of the Page that will now contain your Posts. If you do not select an option here, then your Posts will only be accessible via other navigation features such as category, calendar, or archive links.

Reading Options Subpanel twiki

Turbocharged option

Turbocharged CMS has a variation on the static home page, but references the builtin WP 2.1 option. It works by creating a home.php file and then setting a filter to display the home page or the blog page

How it works

It’s rather simple. When home.php exists, it’s automatically loaded in lieu of index.php when someone visits the front page of your blog.

This version of home.php does one clever trick: it redirects visitors on your main URL to your static page. But the story doesn’t end here — otherwise you’d be left without a way for readers to read your blog front page.

To perform the redirection accurately, home.php first examines the WordPress engine to look for query arguments:

* If the front page is visited with no arguments, then the reader is redirected to the designated static home page
* Otherwise, the standard arguments are processed like usual, and index.php is called on to perform the display of the requested posts
Creating a static front page on your WordPress blog

This sounds good until you remember the WordPress twiki page warning not to use home.php and blog.php at the same time.

So what is the solution?

What is the best method to create the blog index page once you’ve added a static home page? I’ve created the new blog template for the blog section as described in the previously mentioned twiki. I created a new page: blogs and set that page to use this new template. I have gone into the reading options panel and selected this page to be the posts page index. The result? A 404 page. Sigh.

Have you found a fix for this?