WordPress 3’s New Menus

WordPress 3 has brought forth some new tricks like the customizable navigation menus which make it very easy to set up your own menus using a GUI. You can define and drag-and-drop the order of the menu items, and WordPress will output them as an unordered list (<ul><li>…</li></ul>).

Rather than try to explain it myself, just head over to Justin Tadlock’s post about the menus.

One missing piece was filled by John The Developer where I needed to add a custom class to the last menu item.  I was displaying a menu horizontally and using “margin-right” to space the menu items (inside <li> tags) apart, but I didn’t want that margin on the last item.  Here is John’s 8 lines of code solution.

Then in my CSS file, I defined “last-item” class as:

li.last-item {
     margin-right: 0 !important;
}

I did try using the pseudo-class of li:last-child which worked fine in Chrome and Firefox, but it did not work for Internet Explorer because IE has yet to recognize pseudo-classes.

Update Bonus: Want to add some collapsing menu goodness? Check out Daneomatic.com’s post.

FeedWordPress making invalid RSS?

As I mentioned in my last post, I’m using FeedWordpress to syndicate affiliate links.  I turned on the option to have the permalink to the affiliated articles to go straight to the affiliate link instead of the page (that may be bad for SEO, but one less click for visitors).

The problem was the RSS feed became invalid because of the affiliate link.  So I need to encode the link.  So on line 590 of feedwordpress.php, I changed

$uri = get_syndication_permalink();

to

$uri = esc_attr(get_syndication_permalink());

The esc_attr function is a built in WordPress function that converts certain characters to their HTML encoded equivalents. Such as “&” becomes “&amp;”.

Everything seems to be working fine now.

Taming Multiple Loops

So I’m calling a second query inside the Loop, but after the second query ends, the primary loop should take over, right?  Not in this case.

Turned out I needed to call the function wp_reset_query(); after the secondary loop.

Here was my setup: in the page.php template, I wanted a sidebar that displayed the content of another page.  The reason is some pages will need to have the same information displayed on the side, and I wanted to be able to control this on a per page basis. (There is probably a plugin for this, but programming is so much more fun.)

So in the page editor, I set up a custom meta called “sidebar-post” and set it to the path of the page I wanted to be displayed. Back in the page.php template, I set up the WP_Query call like so:

     $sidebar_post = get_post_meta($post->ID, ‘sidebar-post’, false);
     foreach ($sidebar_post as $sidebar_postitem) {
          $sidebar_querystring = ‘pagename=’.$sidebar_postitem;
          $sidebar_query = new WP_Query($sidebar_querystring);
          while ($sidebar_query->have_posts()) : $sidebar_query->the_post(); 
               the_content();
          endwhile;
     }

I set up the code so if I wanted to, I could show more than one page. Now I was able to get this page to display in the sidebar just fine, but page contents appeared again where the primary page content was supposed to be.

I tried a trick described here under the “Multiple Loops Example 2” where you set the primary query to a temporary variable, then set it back after the secondary loop.

     <?php $temp_query = $wp_query; ?>

     <!–  secondary loop processing //—>

     <?php $wp_query = $temp_query; ?>

But that didn’t work.  The secondary query was still in control.

So after some searching I found the wp_reset_query(); function which “destroys the previous query used on a custom Loop”.

So now my code looked like this:

     $sidebar_post = get_post_meta($post->ID, ‘sidebar-post’, false);
     foreach ($sidebar_post as $sidebar_postitem) {
          $sidebar_querystring = ‘pagename=’.$sidebar_postitem;
          $sidebar_query = new WP_Query($sidebar_querystring);
          while ($sidebar_query->have_posts()) : $sidebar_query->the_post(); 
               the_content();
          endwhile;
     }
     wp_reset_query();

And, just as advertised, it killed the secondary loop query and the primary content was displaying properly.

On a side note, I am planning on getting this work with both pages and posts.