How to set post template for all the posts in one category

Posted in: Newspaper
Post count: 26

Hi everyone. How can I bind a post template to each and every post in the certain category? So all posts in one category will have different template, than the rest of the posts in other categories. Is it possible? Thank you.

Post count: 20685

Hi,

Something like that cannot be done at the moment, as far as I know an option like this is considered for implementation in a future update. But at the moment a post template can only be set per post or for all posts at once if this option is selected in the post edit screen – http://prntscr.com/mwrjmb
Sorry for the inconvenience.

Thanks

Post count: 4

I have the solution for the problem, is a little ”handmade” but it works.
you have to edit the function get_post_meta_array in \includes\wp_booster\td_util.php row 1540 whit this enhanced function:

static function get_post_meta_array($post_id, $key) {
		$lookat=array(
		        //id_category=>"single_template_xx"
			2247=>"single_template_3",
			4360=>"single_template_10"
		);
        $post_meta = get_post_meta($post_id, $key, true);
	$category = get_the_category($post_id)[0];
	$first_category = $category->term_id;
		
        if (!is_array($post_meta)&&!isset($lookat[$first_category])) {
	    $post_meta=array();
            return $post_meta; 
         }elseif(!is_array($post_meta)&&isset($lookat[$first_category])){
            $post_meta=array();
	    $post_meta["td_post_template"]=$lookat[$first_category];
            return $post_meta;
         }
		
        return $post_meta;

    }

You have to populate the array with the association category_id -> post_template, the variables are single_template_1 to single_template_13.
Custom post settings will be maintained.
Hope to be useful

  • This reply was modified 7 years by maxx1985.
  • This reply was modified 7 years by maxx1985.
Post count: 4

The code was a mess 🙂


static function get_post_meta_array($post_id, $key) {
$lookat=array(
2247=>"single_template_3",
4360=>"single_template_10"
);
$post_meta = get_post_meta($post_id, $key, true);
$category = get_the_category($post_id)[0];
$first_category = $category->term_id;
if (!is_array($post_meta)&&!isset($lookat[$first_category])) {
$post_meta=array();
return $post_meta;
}elseif(!is_array($post_meta)&&isset($lookat[$first_category])){
$post_meta=array();
$post_meta["td_post_template"]=$lookat[$first_category];
return $post_meta;
}
return $post_meta;
}

Post count: 20685

Hi,

Nice, thank you for posting the code you created and sorry for the inconvenience again. There will be an option for this implemented in a future update, can’t say when exactly.

Thanks

Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic.