Guest User

Untitled

a guest
Jan 24th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <?php
  2. // Look for a direct sidebar to this page
  3. $querystr = "
  4. SELECT *
  5. FROM $wpdb->posts
  6. WHERE post_title = 'Sidebar'
  7. AND post_parent = $post->ID
  8. AND post_status = 'publish'
  9. AND post_type = 'page'
  10. ORDER BY post_date DESC
  11. ";
  12. $direct_sidebar = $wpdb->get_results($querystr, OBJECT);
  13.  
  14. if($direct_sidebar) : // found a direct sidebar, set up postdata and display it
  15. global $post;
  16. foreach($direct_sidebar as $post) :
  17. setup_postdata($post);
  18. echo '<div class="sidebar">';
  19. the_content(); // display user's sidebar content
  20. echo '</div>';
  21. endforeach;
  22. else : // no direct sidebar, check for a parent page sidebar
  23. $ancestors=get_post_ancestors($post->ID);
  24. $root=count($ancestors)-1;
  25. $parent = $ancestors[$root];
  26.  
  27. $querystr = "
  28. SELECT *
  29. FROM $wpdb->posts
  30. WHERE post_title = 'Sidebar'
  31. AND post_parent = $parent
  32. AND post_status = 'publish'
  33. AND post_type = 'page'
  34. ORDER BY post_date DESC
  35. ";
  36. $parent_sidebar = $wpdb->get_results($querystr, OBJECT);
  37. if($parent_sidebar) :
  38. global $post;
  39. foreach($parent_sidebar as $post) :
  40. setup_postdata($post);
  41. echo '<div class="sidebar">';
  42. the_content();
  43. echo '</div>';
  44. endforeach;
  45. endif;
  46. endif;
  47.  
  48. ?>
Add Comment
Please, Sign In to add comment