Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php if (is_sticky()) : ?>
  2.  
  3. <div class="col-md-12 box-01">
  4. <div class="content">
  5.  
  6. <?php endif; ?>
  7.  
  8. if ( is_sticky()
  9. && !is_paged()
  10. ) {
  11. // Add your styling here
  12. }
  13.  
  14. add_action( 'pre_get_posts', function ( $q )
  15. {
  16. if ( $q->is_home() // Only target the home page
  17. && $q->is_main_query() // Only target the main query
  18. && $q->is_paged() // Only target paged pages
  19. ) {
  20. // Remove stickies
  21. $q->set( 'post__not_in', get_option( 'sticky_posts' ) );
  22. }
  23. });
  24.  
  25. add_filter( 'found_posts', function( $found_posts, WP_Query $q )
  26. {
  27. if ( $q->is_home()
  28. && $q->is_main_query()
  29. && !$q->is_paged()
  30. ) {
  31. // Get the sticky posts array
  32. $stickies = get_option( 'sticky_posts' );
  33.  
  34. // Get all the post ID's from the first page
  35. $ids = $q->posts;
  36.  
  37. // Remove all the current ids from the current sticky posts array
  38. $stickies = array_diff( $stickies, $ids );
  39.  
  40. $found_posts = $found_posts - count( $stickies );
  41. }
  42.  
  43. return $found_posts;
  44. }, 10, 2 );
  45.  
  46. add_action( 'pre_get_posts', function ( $q )
  47. {
  48. if ( $q->is_home()
  49. && $q->is_main_query()
  50. ) {
  51. $q->set( 'ignore_sticky_posts', 1 );
  52. $q->set( 'post__not_in', get_option( 'sticky_posts' ) );
  53.  
  54. add_filter( 'the_posts', function( $posts, WP_Query $q )
  55. {
  56. // Only do this for the first page
  57. if ( $q->is_paged() )
  58. return $posts;
  59.  
  60. $stickies = get_option( 'sticky_posts' );
  61. // Check if we have stickies
  62. if ( !$stickies )
  63. return $posts;
  64.  
  65. $args = [
  66. 'posts_per_page' => count( $stickies ),
  67. 'post__in' => $stickies,
  68. ];
  69. $sticky_posts = get_posts( $args );
  70.  
  71. // Merge the array
  72. $posts = array_merge( $sticky_posts, $posts );
  73.  
  74. return $posts;
  75. }, 10, 2 );
  76. }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement