Paging in wordpress themes
-
First of all I apologise if I appear grumpy but I am!
Surely it cannot be this <censored> hard to get paging working as I need in WordPress. I suspect that my situation may be unusual.
I have different category templates that show two, thee or four columns per page and I can choose between them using the Category Templates plug in which works very well.
The problem comes when trying to add paging to my templates. When I click “Next” (next_posts_link()) I get a not found, which I can correct by setting the “Blog pages show at most” option. Unfortunately that only works for one of my templates.
With a bit of digging I found I could add an action to modify the query so I have added the following in to my functions.inc
add_action( 'pre_get_posts', 'modify_query' ); function modify_query($query) { global $prime_cols; if (!$query->is_category()) { return; } if ( method_exists( $query, 'is_main_query' ) ) { if (!$query->is_main_query()) { return; } } else { global $wp_the_query; if ($query !== $wp_the_query) { return; } } var_dump($prime_cols); $query->set('posts_per_page', $prime_cols); }
and I have added the following to my two column template
global $prime_cols; $prime_cols = 2;
The var_dump in the modify_query routine shows that the $prime_cols variable is indeed (int) 2 but the $query->set only seems to work if I hard code the value instead of using the variable.
Where am I going wrong?
I should add that this is being done using a custom query
$cat = get_query_var('cat'); $category = get_category ($cat); $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $temp = $wp_query; // Search $wp_query = new WP_Query(); $wp_query->query('post_type=page&post_status=publish&category_name=' . $category->slug . '&showposts=2&posts_per_page=2' . '&paged=' . $page);
And it doesn’t appear to be the first time it is getting in to the modify query. The fist time (a default call?) it is showing $prime_cols as being null and I suspect this is the only time it is setting the <censored> value.
- The topic ‘Paging in wordpress themes’ is closed to new replies.