Category: WordPress

WordPress hack: Force wp_list_pages() to print current_page_item class

The wp_list_pages() function displays a list of WordPress pages as links and is often used to display navigation menus. When it prints the list of links, it adds a CSS class current_page_item to the list item tag of the current page. This allows custom styling to be applied to the link e.g. to highlight a link to indicate the page that the visitor is on.

The limitation with this is that it only works if you are viewing a page or an attachment, or are on the posts page (as set in Settings > Reading).

I was working on an archive page for a custom taxonomy and wanted to have the current_page_item CSS class added to the list item of a particular link of my choice. After poking around a bit in the wp_list_pages() code listing, I discovered it’s possible to hack the $wp_query object to make wp_list_pages() do what I wanted it to.

Here’s how:

// Get the ID of the link you want highlighted. Either:
// - hardcode it (as below)
// - or query the database to get it
$the_id = "1";


// Make a copy of the $wp_query object
$temp_query = clone $wp_query;

// Trick WordPress into thinking we're on a page with ID $the_id
$wp_query->is_page = 1;
$wp_query->queried_object_id = $the_id;

// Display pages
wp_list_pages("title_li=");

// Reset $wp_query
$wp_query = clone $temp_query;

Notes:

  • $wp_query->is_page needs to be set to 1 (true) because wp_list_pages() does not set a current page unless is_page() or is_attachment() or $wp_query->is_posts_page returns true.
  • If you only want this code to run when you are on certain page you can wrap it in an if statement that checks a WordPress conditional tag. In my case I only wanted to run this code when a taxonomy archive was being displayed so I used is_tax().
  • See http://www.jenst.se/2008/03/17/wordpress-get-id-by-post-or-page-name/ on how to query the database by post/page name to get an ID.

Set WordPress custom post types order in admin

One of the long-awaited new features of WordPress 3.0 is custom post types.

By default, custom posts with the post capability_type (as opposed to page, attachment, revision, or nav-menu-item) are listed newest to oldest in the WordPress backend. This makes sense if these are blog posts, but if they are some other content type (such as quotes, movies, books or contacts) it is often desirable to have them listed alphabetically.

To achieve this, add the following code to your functions.php file, replacing POST_TYPE (line 7) with the name of the post type that you want to have listed alphabetically.

function set_custom_post_types_admin_order($wp_query) {
  if (is_admin()) {

    // Get the post type from the query
    $post_type = $wp_query->query['post_type'];

    if ( $post_type == 'POST_TYPE') {

      // 'orderby' value can be any column name
      $wp_query->set('orderby', 'title');

      // 'order' value can be ASC or DESC
      $wp_query->set('order', 'ASC');
    }
  }
}
add_filter('pre_get_posts', 'set_custom_post_types_admin_order');

Import Facebook note comments into WordPress using Facebook CommentsTNG

Facebook allows you to quickly and easily import an external blog from another website. See the Facebook help pages if you want to find out how to do this.

This is great, but after a blog post had been imported as a note, Facebook users are likely to comment on it in Facebook. If you are using blogging with WordPress and want to have all your comments in one place, there is a WordPress plugin that can help you: Facebook CommentsTNG. It will automatically scan your Facebook notes for imported WordPress blog posts and bring the comments back into WordPress.

This is an excellent plugin which builds on the idea of the Facebook Comments plugin, which no longer works (probably due to changes in way Facebook notes are marked up).

Check out the author’s page for the plugin: Facebook CommentsTNG.