Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • Do you have JavaScript blocked in the browser?

    Thread Starter elija

    (@elija)

    OK, I have a solution of sorts. I’m not entirely happy with it but I’m on a deadline. I’ll take the time to share anyway.

    1. Hack the Category Templates plug in so it can identify the template it will use

    2. In pre_get_posts use this to override settings for the default query

    3. No more custom queries (I like this bit)

    So in the Category Templates plug in replace the cat_temp function with

    function cat_temp($template, $include = true) {
    	global $wp_query;
    	$post_obj = $wp_query->get_queried_object();
    
    	if (is_single()) {
    		$data = cat_temp_get_data();
    		$categories = get_the_category($post_obj->ID);
    	} else if (is_category()) {
    		$data = cat_temp_get_data(true);
    		$categories[0] = &get_category($post_obj->cat_ID);
    	}
    	$temp_data;
    
    	// Get templates for post categories
    	foreach((array)$categories as $category) {
    		if ($data[$category->term_id]['template'] != '0') {
    			$temp_data[$data[$category->term_id]['template']] = $data[$category->term_id]['priority']+($category->term_id/80000);
    		}
    	}
    
    	// Get templates for parent categories
    	foreach((array)$data as $key => $cat) {
    		if ($cat['all'] == "all" && $cat['template'] != "0") {
    			$id = (is_single()) ? (int)$cat['id'] : $key;
    			$descendants = get_term_children($id, 'category');
    			if ($descendants && in_category($descendants)) {
    				$temp_data[$cat['template']] = $cat['priority']+($cat['id']/80000);
    			}
    		}
    	}
    
    	//print_r($temp_data);
    
    	// Sort templates by priotiry, and return the one with the highest priority
    	if (is_array($temp_data)) {
    		asort($temp_data);
    		$template = array_shift(array_keys($temp_data));
    	}
    
    	// If currrent post has a template, use that one instead
    	if (is_single()) {
    		$overRule = cat_temp_post_template($post_obj->ID);
    		if ($overRule) $template = $overRule;
    	}
    
    	// Return template path
    	if (!empty($template)) {
    		if ($include) {
    			if (file_exists(TEMPLATEPATH.$template)) {
    				include(TEMPLATEPATH.$template);
    				exit;
    			}
    		} else {
    			return $template;
    		}
    	}
    }

    and use it in the pre_get_posts hook

    add_filter( 'pre_get_posts', 'modify_query' );
    function modify_query($query)
    {
    
    	if (!$query->is_category()) {
    		return;
    	}
    
            global $wp_the_query;
    	if ( method_exists( $query, 'is_main_query' ) ) {
    		if (!$query->is_main_query()) {
    			return;
    		}
    	} else {
    
    		if ($query !== $wp_the_query) {
    			return;
    		}
    	}
    
    	$category = $wp_query->get_queried_object();
    	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$query->set('post_type', 'page');
    	$query->set('category_name', $category->slug);
    	$query->set('paged', $page);
    
    	$template = cat_temp("", false);
    	switch ($template) {
    		case '/two-col.php':
    			$query->set('posts_per_page', 2);
    			$query->set('showposts', 2);
    			break;
    		case '/three-col.php':
    			$query->set('posts_per_page', 3);
    			$query->set('showposts', 3);
    			break;
    		case '/four-col.php':
    			$query->set('posts_per_page', 4);
    			$query->set('showposts', 4);
    			break;
    	}
    }

    If experienced WordPress developers don’t cough up their spines in this thread, I’ll send the Category Template changes upstream…

    Thread Starter elija

    (@elija)

    So… I think it goes like this.

    Page one:
    Run a default query ($prime_cols = NULL) this returns a resultset as it is starting at 0
    Runs my custom query ($prime_cols = 2) as it is starting at 0, paging correctly set using variable

    Page Two:
    Run a default query ($prime_cols = NULL) this returns null / false as it is starting at 10 and only 3 items exist
    Shows 404
    Never runs custom query.

    I guess I need to over-ride the default query rather than running a custom one. Is this possible?

    I’m not sure that “default” is the right term

    Thread Starter elija

    (@elija)

    The var_dump shows it as an int(2), int(3) or int(4) depending on which template I am in and to me that would more than suggest that is does exist at that point.

    It also shows a NULL which is coming out previously (which may be where you were referring to)


    See the top left and bottom right of the image linked for what I mean

    What is the fix?

    Thread Starter elija

    (@elija)

    I suppose I should have said that this patch is for V1.03 of the plug-in

    I have written a patch that implements this in version 1.03

    Link

Viewing 6 replies - 1 through 6 (of 6 total)