Guest User

Untitled

a guest
Oct 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. add_filter( 'single_template', 'my_single_template' );
  2. function my_single_template($single_template)
  3. {
  4. if (in_category(1)) {
  5. $file = get_template_directory().'/single-cat-1.php';
  6. if ( file_exists($file) ) {
  7. return $file;
  8. }
  9. }
  10. return $single_template;
  11. }
  12.  
  13. function wpse_category_single_template( $single_template ) {
  14. global $post;
  15. $all_cats = get_the_category();
  16.  
  17. if ( $all_cats[0]->cat_ID == '1' ) {
  18. if ( file_exists(get_template_directory() . "/single-cat1.php") ) return get_template_directory() . "/single-cat1.php";
  19. } elseif ( $all_cats[0]->cat_ID == '2' ) {
  20. if ( file_exists(get_template_directory() . "/single-cat2.php") ) return get_template_directory() . "/single-cat2.php";
  21. }
  22. return $single_template;
  23. }
  24. add_filter( 'single_template', 'wpse_category_single_template' );
  25.  
  26. function show_template() {
  27. global $template;
  28. print_r($template);
  29. }
  30. add_action( 'wp_head', 'show_template' );
  31.  
  32. function wpse_category_single_template( $single_template ) {
  33. global $post;
  34. $all_cats = get_the_category();
  35.  
  36. if ( in_category(6) ) {
  37. if ( file_exists(get_template_directory() . "/page-custom.php") ) {
  38. return get_template_directory() . "/page-custom.php";
  39. } else {
  40. return get_template_directory() . "/page.php";
  41. }
  42. }
  43. return $single_template;
  44. }
  45. add_filter( 'single_template', 'wpse_category_single_template' );
  46.  
  47. add_filter( 'single_template', function ( $template )
  48. {
  49. // Get the current single post
  50. $post_id = $GLOBALS['wp_the_query']->get_queried_object_id();
  51.  
  52. // Test to see if our post belongs to category 1
  53. if ( !in_category( 1, $post_id ) ) {
  54. return $template;
  55. }
  56.  
  57. // Our post is attached to category 1, lets look for single-1.php
  58. $locate_template = locate_template( 'single-1.php' );
  59.  
  60. // Test if our template exist, if so, include it, otherwise bail
  61. if ( !$locate_template ) {
  62. return $template;
  63. }
  64.  
  65. return $locate_template;
  66. });
Add Comment
Please, Sign In to add comment