Posts by date

Add the_shortlink()

Posted by The Doctor on Jul 22, 2010 with No Comments | Short Link
Last modified by The Doctor on September 9, 2010
in Functions
as ,

I believe there are many reasons for themes to use this function or add it to your WordPress web site. I will briefly discuss a couple of the ones that come to mind. Note, if you are using the default permalink structure this may not be for you as the_shortlink() creates a URL that looks just like it.1

If you are new to WordPress and are still experimenting with the permalink structure to best fit your needs, you might consider using this function to create internal links for your site. Using the URL generated by the_shortlink() will always resolve correctly, using the verbose URL of the permalink structure may cause issues if you change the structure and do not manually edit all the internal links you have previously posted.

Please note, by “internal links” I am refering to the links used within the content of a post or page on your site pointing to another post or page on your site. A WordPress installation has the “smarts” to correctly resolve the links it generates if you change the permalink structure but it will not go back and “fix” anything you manually typed yourself as part of your page or post entry.

There is great potential for the user, the designer, and the developer using this function. I recommend adding it to all themes. Here is the most basic default usage of the_shortlink():

<?php the_shortlink(); ?>

This is the code I have decided to use on this site:

<?php the_shortlink( __('Short Link'), '', ' &#124; ', '' ); ?>

… which you can see just after the comments or subscribe link in the post meta details.

Of course you can always use the ‘Get Shortlink’ button found on the page and post administration panels to get your shortlink, but making use of this single line of code allows your readers to gain that same benefit, too.

  • NB: If you have the WordPress.com Stats plugin installed and activated, the_shortlink() function will use the wp.me link shortening service; and, it will retain all of the usefulness I noted above.
  1. Sept. 9, 2010: Recent testing has shown the “Get Shortlink” button on the Edit Post panel will not appear unless a permalink structure other than default is used. This also affects the_shortlink() display functionality.

Upgrade wp_list_categories() to wp_nav_menu()

Posted by The Doctor on Jul 20, 2010 with 6 Comments | Short Link
in Functions
as , , ,

In this third installment in the upgrade to wp_nav_menu() series of posts, I will be dealing with moving from wp_list_categories() to wp_nav_menu().

Let’s start with the same basic outline and list the default options for each function. We will start with wp_nav_menu() 1 first:

wp_nav_menu( array(
    'menu'              => '',
    'container'         => 'div',
    'container_class'   => '',
    'container_id'      => '',
    'menu_class'        => 'menu',
    'menu_id'           => '',
    'echo'              => true,
    'fallback_cb'       => 'wp_page_menu',
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => '',
    'theme_location'    => ''
    ) );

We follow with wp_list_categories() 2, 3 which is often wrapped in a <ul class=”menu”> tag:

<ul class="menu">
wp_list_categories( array(
    'include'           => '',    
    'exclude'           => '',
    'exclude_tree'      => '',
    'child_of'          => 0,
    'hide_empty'        => 1,
    'orderby'           => 'name',
    'order'             => 'ASC',
    'use_desc_for_title'=> 1,
    'number'            => NULL,
    'hierarchical'      => true,    
    'show_count'        => 0,    
    'pad_counts'        => 0,
    'style'             => 'list',
    /* 'style' set to list "creates list items for an unordered list" */
    'show_option_all'   => '',
    'show_option_none'  => __('No categories'),
    'show_last_update'  => 0,
    'feed'              => '',
    'feed_type'         => '',
    'feed_image'        => '',
    'current_category'  => 0,
    'taxonomy'          => 'category',
    'title_li'          => __( '' ),
    /* 'title_li' set to '' for menus from the default 'Categories' */
    'echo'              => 1,
    'depth'             => 0,
    'walker'            => 'Walker_Category'    
    ) );
</ul>

Here is the break-down of the wp_list_categories() default options from above:

  • 'include' – not used
  • 'exclude' – not used
  • 'exclude_tree' – not used
  • 'child_of' – not used
  • 'hide_empty' – not used
  • 'orderby' – not used
  • 'order' – not used
  • 'use_desc_for_title' – not used
  • 'number' – not used
  • 'hierarchical' – not used
  • 'show_count' – not used
  • 'pad_counts' – not used
  • 'style' – not used
  • 'show_option_all' – not available
  • 'show_option_none' – not available
  • 'show_last_update' – not available
  • 'feed' – not available
  • 'feed_type' – not available
  • 'feed_image' – not available
  • 'current_category' – not available
  • 'taxonomy' – not available
  • 'title_li' – not used
  • 'echo' – similar to wp_nav_menu(); true versus 1 as value
  • 'depth' – same as wp_nav_menu()
  • 'walker' – see below for additional notes

Although there are great many options available to be used with wp_list_categoires() most are not used (as noted in the list above) in place of the user interface of wp_nav_menu() found under Appearance | Menu in the dashboard. The options that are marked as “not available” are currently not directly supported by wp_nav_menu() without the application of filters. The last few options remaining match up almost exactly with wp_page_nav() in a similar fashion as was shown with wp_list_pages().

Just like the wp_list_pages() consideration, the wp_nav_menu() fallback_cb option may be set to 'fallback_cb' => 'wp_list_categories'. This backward compatibility may also be addressed with a custom function as was noted, too.

Note: In most cases you would leave the wp_nav_menu() 'walker' option set to its default NULL although wp_list_categories() uses its own default walker class.

Upgrade wp_list_pages() to wp_nav_menu()

Posted by The Doctor on Jul 16, 2010 with 1 Comment | Short Link
in Functions
as , , ,

Following in the series dealing with upgrading to the wp_nav_menu() function in WordPress 3.0 from menus derived from the template tags: wp_page_menu(); wp_list_pages(); and, wp_list_categories(). This post will be covering the options involved in updating to wp_nav_menu() from wp_list_pages().

Like the last post in the series we will look at the default arguments used by each function, starting with wp_nav_menu() 1:

wp_nav_menu( array(
    'menu'              => '',
    'container'         => 'div',
    'container_class'   => '',
    'container_id'      => '',
    'menu_class'        => 'menu',
    'menu_id'           => '',
    'echo'              => true,
    'fallback_cb'       => 'wp_page_menu',
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => '',
    'theme_location'    => ''
    ) );

The following is a default “menu” version of wp_list_pages() 2, 3, often wrapped within a <ul class=”menu”> tag:

<ul class="menu">
<?php wp_list_pages( array(
    'sort_column'       => 'menu_order, post_title',
    'include'           => '',    
    'exclude'           => '',
    'exclude_tree'      => '',
    'child_of'          => 0,
    'show_date'         => '',
    'date_format'       => get_option('date_format'),
    'title_li'          => __(''),
    /* 'title_li' set to '' for menus from the default 'Pages' */
    'authors'           => '',
    'echo'              => 1,
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => ''
    ) ); ?>
</ul>

Here is the break-down of the wp_list_pages() default options from above:

  • 'sort_column' – not used
  • 'include' – not used
  • 'exclude' – not used
  • 'exclude_tree' – not used
  • 'child_of' – not used
  • 'show_date' – not used
  • 'date_format' – not used
  • 'title_li' – not used, see below for additional notes
  • 'authors' – not used
  • 'echo' – similar to wp_nav_menu(); true versus 1 as value
  • 'link_before' – same as wp_nav_menu()
  • 'link_after' – same as wp_nav_menu()
  • 'depth' – same as wp_nav_menu()
  • 'walker' – same as wp_nav_menu()

Similar to wp_page_menu(), the ‘sort_column’, ‘include’, ‘exclude’, ‘exclude_tree’, and ‘child_of’ are replaced via the end-user’s specific choices. The ‘show_date’ and its related ‘date_format’ option, as well as the ‘author’ argument, are not available within the default wp_nav_menu() structure. Once again, the balance of the default options in wp_list_pages() matches the defaults found in wp_nav_menu().

Also consider setting the wp_nav_menu() fallback_cb option to 'fallback_cb' => 'wp_list_pages'. This backward compatibility may also be addressed with a custom function as posted by Nicolas Kuttler.

Note: If the 'title_li' => __('') option was being used it may also require the removal of the wrapping <ul> tag in the existing menu structure created with wp_list_pages().

Upgrade wp_page_menu() to wp_nav_menu()

Posted by The Doctor on Jul 12, 2010 with 11 Comments | Short Link
Last modified by The Doctor on July 25, 2010
in Functions
as , , ,

Upgrading from wp_page_menu() to wp_nav_menu() may be as easy as a simple find and replace operation.