I found two pieces of code for WordPress that might come in handy from time to time.

Custom Post Types pagination for the homepage

It appears there’s a bug in WP 3.0 with the pagination on the homepage for custom post types.

When we create an archive page for a custom post type we make a page template where we use query_posts() to list them.

However, when we make try to put the pagination in place using get_query_var(‘paged’) it fails.

global $paged;
 
//this code doesn't work
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 
//this code works
if ( get_query_var('paged') )
	$paged = get_query_var('paged');
elseif ( get_query_var('page') )
	$paged = get_query_var('page');
else
	$paged = 1;
 
$args=array(
	'post_type' => 'cpt',
	'paged' => $paged,
	'posts_per_page' => 10,
	'post_status' => 'publish',
);
query_posts($args);
Thanks go to Devin Price for pointing me in the right direction.

Extend category permalinks to query for tags

While most people don’t need this, from time to time you might want to have query posts that are in category ‘x’ and are tagged with ‘y’. WordPress can query for that very easily, all we need to do is add the permalinks structure: www.example.com/category/x/tag/y/

function extend_category_tag_flush_rewrite_rules() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}
add_action('init', 'extend_category_tag_flush_rewrite_rules');
 
function extend_category_tag_add_rewrite_rules($wp_rewrite) {
	$rules = array();
	$structures = array(
		$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_tag_permastruct(),
	);
	foreach( $structures as $s ){
		$rules += $wp_rewrite->generate_rewrite_rules($s);
	}
	$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'extend_category_tag_add_rewrite_rules');

You can add this code in your function.php file the theme came with.

Code based on: sltaylor.co.uk/blog/
By This entry was posted in Wordpress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted May 29, 2011 at 8:07 pm | Permalink

    Hello everyone!

    Check out. fantastic

    kostka granitowa kraków

One Trackback

  1. By links for 2010-09-20 on September 20, 2010 at 10:01 pm

    [...] WordPress pieces of code of the month (tags: webdesign wordpress custom pagination) Socio-Encapsule this: [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

* Copy this password

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>