Archive widget: limit the number of months to 12

If you want to limit the number of months to only 12 for your Archive widget in your theme sidebar, use the widget_archives_args filter to add the archives limit.

Insert the following code into the functions.php file of your theme.

function my_limit_archives( $args ) {
    $args['limit'] = 12;
    return $args;
}
add_filter( 'widget_archives_args', 'my_limit_archives' );

And to limit the number of months in Archives widget drop-down, use the following drop-down filter by replacing the last line of the above code.

add_filter( 'widget_archives_dropdown_args', 'my_limit_archives' );

We hope this helps!

Limit the number of tags in your WordPress tag cloud widget

If your WordPress-powered site has a lot of tags (and growing), you will probably be struggling with a messy-looking list inside the tag cloud widget. But you would rather show a decent number of tags instead of removing the widget altogether, right?

Place the following code in your theme’s functions.php file which should resolve this issue.

//Register tag cloud filter callback
add_filter('widget_tag_cloud_args', 'tag_widget_limit');

//Limit number of tags inside widget
function tag_widget_limit($args){

 //Check if taxonomy option inside widget is set to tags
 if(isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag'){
  $args['number'] = 10; //Limit number of tags
 }

 return $args;
}

Let us know whether this works out for you just fine!

Native pagination without plugins in WordPress

Any blogger who has been blogging on WordPress will be familiar with pagination plugins such as WP-PageNavi, which displays pagination like this:

pagination

But not everyone knows that WordPress has a built-in function (starting with version 2.1) which implements almost the same functionality. Thus the need for a plugin disappears. Continue reading

Incoming search terms:

  • wordpress pagination plugin
  • wordpress pagination without plugin

WordPress: Add rel=”nofollow”

Add the following lines to your Theme’s template functions.php file or create it as a Plugin by filling the additional Standard Plugin Information:

function rel_nofollow( $content ) {
    return preg_replace_callback( '/<a[^>]+/', 'rel_nofollow_callback', $content );
}
add_filter( 'the_content', 'rel_nofollow', 99999 );

function rel_nofollow_callback( $matches ) {
    $link = $matches[0];

    $exclude = '('. home_url() .'|http://([^.]+.)?(wp.org|wp.com))';

    if ( preg_match( '#href=S('. $exclude .')#i', $link ) )
        return $link;

    if ( strpos( $link, 'rel=' ) === false ) {
        $link = preg_replace( '/(?<=<as)/', 'rel="nofollow" ', $link );
    } elseif ( preg_match( '#rel=S(?!nofollow)#i', $link ) ) {
        $link = preg_replace( '#(?<=rel=.)#', 'nofollow ', $link );
    }

    return $link;	
}

From the snippet above:

  1. Search for <a> tag
  2. Don’t do 3) and 4) if pattern matches home URL, wp.org and wp.com
  3. If rel attribute doesn’t exist, add rel=”nofollow”
  4. If rel attribute exist but no “nofollow” attribute value, add it

WordPress: Better previous and next post links

To be used on single post, the snippet below will not output unnecessary markup if there’s:

  1. only 1 published post.
  2. no post that is adjacent (previous or next) to current post.
<?php php if ( get_adjacent_post( false, '', false ) ||  get_adjacent_post( false, '', true ) ) : ?>
  <div class="navigation">
    <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">&larr;</span> %title' ); ?>
    <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">&rarr;</span>' ); ?>
  </div>
<?php endif; ?>