11 May 2010

Force WordPress network to use sub-directories

In WordPress 3.0, we have the ability to create a network of sites. But if your install is over 1 month old, the sites in your WordPress network must use sub-domains because of permalink problems with “/blog/” from the main site. This is how you can force the sites in your WordPress network to use sub-directories during the Network installation process.

Add the following lines to your current activated WordPress Theme’s template file, preferably your Theme’s template functions.php file:

add_filter( 'allow_subdirectory_install',
	create_function( '', 'return true;' )
	);

You can remove the lines above once you done enabling the Network feature.

Creating this into a plugin is not an option, since all plugins are required to be deactivated before enabling the Network feature. Please read on how to Create A Network in the WordPress Codex and ticket #12931 for more information on why it is block by default.

23 Apr 2010

WordPress: Conditional Tags Test

Conditional Tag test

Having trouble identify or determine what conditions that a page matches?

Add the following Conditional Tags test snippet to your WordPress Theme’s template file, preferably your Theme’s template sidebar.php file:

<?php
$conditions = array(
  'is_home()',
  'is_front_page()',
  '$wp_query->is_posts_page',
  'is_page()',
  'is_single()',
  'is_singular()',
  'is_attachment()',
  'is_search()',
  'is_archive()',
  'is_category()',
  'is_tag()',
  'is_tax()',
  'is_author()',
  'is_day()',
  'is_month()',
  'is_year()',
  'is_time()',
  'is_date()',
  'is_paged()',
  'is_comments_popup()',
  'is_preview()',
  'is_404()',
);

echo '<h2>Conditional Tag</h2><ul>';
  $i = 0;
  foreach ( $conditions as $condition ) :
    $condition = str_replace('()', '', $conditions[$i]);

    switch ( $condition ) :
      case 'is_front_page' :
        $query = is_front_page();
        break;
      case '$wp_query->is_posts_page' :
        $query = $wp_query->is_posts_page;
        break;
      default :
        $query = $wp_query->$condition;
        break;
    endswitch;

    echo '<li><code>' . $conditions[$i] . '</code>';
    echo $query ? ': <span style="color:green">true</span>' : ': <span style="color:red">false</span>';
    echo '</li>';

    $i++;
   endforeach;
echo '</ul>';
?>

Note: Currently, there’s no is_posts_page() conditional tag yet.

19 Apr 2010

WordPress: Displays the contents of static page Posts page

By default, if you choose a static Page for Posts page to show your latest Posts, the original content of that Page will not be displayed.

This is how you can display the content of the Page along with the latest Posts.

Add the following lines to your WordPress Theme’s template index.php file, preferably before the loop:

<?php
if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
	$page_for_posts_id = get_option('page_for_posts');
	setup_postdata(get_page($page_for_posts_id));
?>
	<div id="post-<?php the_ID(); ?>" class="page">
		<div class="entry-content">
			<?php the_content(); ?>
			<?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
		</div>
	</div>
<?php
	rewind_posts();
endif;
?>
18 Apr 2010

WordPress: Limit posts per page

Choose and add the following lines to your WordPress Theme’s template functions.php file (For example, functions.php file for a Theme named “default” would probably reside in the directory wp-content/themes/default/functions.php):

  • function limit_posts_per_page() {
    	if ( is_category() )
    		return 2;
    	else
    		return 5; // default: 5 posts per page
    }
    
    add_filter('pre_option_posts_per_page', 'limit_posts_per_page');
    
  • function limit_posts_per_archive_page() {
    	if ( is_category() )
    		set_query_var('posts_per_archive_page', 2); // or use variable key: posts_per_page
    }
    
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');
    

The code example above is for showing 2 posts per page when any Category archive page is being displayed. Same results in two different way.

For more possible page conditions, please refer to the Conditional Tags page in the WordPress Codex.

12 Apr 2010

WordPress [gallery]: Force n Number of Columns

If you use [gallery] shortcode without setting the columns value, the default is set to 3. Some WordPress theme can either fit 3 or more column per row or less. Below is an example to force WordPress [gallery] columns to 2 to any post or page that uses [gallery] shortcode:

function gallery_columns($content){
	$columns = 2;
	$pattern = array(
		'#(\[gallery(.*?)columns="([0-9])"(.*?)\])#ie',
		'#(\[gallery\])#ie',
		'#(\[gallery(.*?)\])#ie'
	);
	$replace = 'stripslashes(strstr("\1", "columns=\"$columns\"") ? "\1" : "[gallery \2 \4 columns=\"$columns\"]")';

	return preg_replace($pattern, $replace, $content);
}

add_filter('the_content', 'gallery_columns');

$columns can be set to any numeric value. If [gallery] columns is set to 0, no row breaks will be included.

Plugin download: Gallery Columns