srikat

single-post.php

Jan 19th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. <?php
  2.  
  3. // Add "has-featured-image" body class if the post has a featured image
  4. add_filter( 'body_class', 'sk_single_post_body_class' );
  5. /**
  6. * Adds a css class to the body element
  7. *
  8. * @param array $classes the current body classes
  9. * @return array $classes modified classes
  10. */
  11. function sk_single_post_body_class( $classes ) {
  12.  
  13. if ( has_post_thumbnail() ) {
  14. $classes[] = 'has-featured-image';
  15. }
  16.  
  17. return $classes;
  18. }
  19.  
  20. add_action( 'get_header', 'sk_layout' );
  21. function sk_layout() {
  22.  
  23. // if the post does not have a featured image, abort.
  24. if ( ! has_post_thumbnail() ) {
  25. return;
  26. }
  27.  
  28. // force full width content
  29. add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
  30.  
  31. // insert featured image and Primary sidebar
  32. add_filter( 'the_content', sk_featured_image_sidebar, 20 );
  33.  
  34. }
  35.  
  36. // Function to insert featured image and Primary sidebar
  37. function sk_featured_image_sidebar( $content ) {
  38.  
  39. // store the Primary sidebar's output in a variable using output buffering
  40. ob_start();
  41. get_sidebar(); // include the sidebar.php template file
  42. $sidebar = ob_get_clean();
  43.  
  44. if ( wp_is_mobile() ) {
  45. // insert featured image after the first paragraph
  46. $content = preg_replace( '/<\/p>/', '</p>' . get_the_post_thumbnail( $post->ID, 'post-single' ), $content, 1 );
  47. } else {
  48. // insert featured image and Primary sidebar after the first paragraph
  49. $content = preg_replace( '/<\/p>/', '</p>' . get_the_post_thumbnail( $post->ID, 'post-single' ) . $sidebar, $content, 1 );
  50. }
  51.  
  52. if ( wp_is_mobile() ) {
  53. return $content . $sidebar;
  54. } else {
  55. return $content;
  56. }
  57.  
  58. }
  59.  
  60. genesis();
Add Comment
Please, Sign In to add comment