Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. main query // normal query
  2. $k=0 // setting iterator
  3. while have posts, the post
  4.  
  5. ---post markup---
  6.  
  7.  
  8. if($k == 4 or $k % 4 == 0) { // whether after x number or after every x number
  9. second query // using WP_Query
  10. while second query has posts, the posts
  11.  
  12. ---custom post type markup // I NEED TO SHOW MORE THAN ONE HERE IF IT IS AFTER EVERY X blogposts
  13.  
  14. endwhile; //second query ends
  15.  
  16. }
  17.  
  18. $k++;
  19. endwhile // main query
  20.  
  21. PPP(Y) = y * floor( ( PPP(X) -1 ) / x )
  22.  
  23. PPP(X)=1, x=3, y=2, PPP(Y) = 2 * floor( (1-1)/3 ) = 2 * 0 = 0
  24. Loop: X
  25.  
  26. PPP(X)=3, x=3, y=2, PPP(Y) = 2 * floor( (3-1)/3 ) = 2 * 0 = 0
  27. Loop: XXX
  28.  
  29. PPP(X)=4, x=3, y=2, PPP(Y) = 2 * floor( (4-1)/3 ) = 2 * 1 = 2
  30. Loop: XXX YY X
  31.  
  32. PPP(X)=11, x=3, y=2, PPP(Y) = 2 * floor( (11-1)/3 ) = 2 * 3 = 6
  33. Loop: XXX YY XXX YY XXX YY XX
  34.  
  35. PPP(X)=12, x=3, y=2, PPP(Y) = 2 * floor( (12-1)/3 ) = 2 * 3 = 6
  36. Loop: XXX YY XXX YY XXX YY XXX
  37.  
  38. PPP(X)=13, x=3, y=2, PPP(Y) = 2 * floor( (13-1)/3 ) = 2 * 4 = 8
  39. Loop: XXX YY XXX YY XXX YY XXX YY X
  40.  
  41. get_template_part( 'content', 'y' );
  42.  
  43. <article>
  44. <header class="entry-header">
  45. <h2 class="entry-title"><?php the_title(); ?></h2>
  46. </header>
  47. <div class="entry-content">
  48. <?php the_content(); ?>
  49. </div>
  50. </article>
  51.  
  52. add_action( 'wp', function(){
  53.  
  54. // We want the injection only on the home page:
  55. if( ! is_home() ) return;
  56.  
  57. // Start the injection:
  58. $inject = new WPSE_Inject( array(
  59. 'items_before_each_inject' => 3,
  60. 'cpt_items_per_inject' => 2,
  61. 'cpt' => 'page',
  62. 'template_part' => 'content-page',
  63. 'paged' => ( $pgd = get_query_var( 'paged' ) ) ? $pgd : 1,
  64. ) );
  65. $inject->init();
  66. });
  67.  
  68. /**
  69. * class WPSE_Inject
  70. *
  71. * Inject custom posts into the main loop, through hooks,
  72. * with only a single WP_Query() call
  73. *
  74. * @link http://wordpress.stackexchange.com/a/141612/26350
  75. *
  76. * Definitions:
  77. * Posts per page: PPP
  78. * Custom post type: Y
  79. * Main query post type: X
  80. * How many Y posts to inject: y
  81. * How many X posts to display before injecting the Y posts: x
  82. *
  83. * Formula:
  84. * PPP(Y) = y * floor( ( PPP(X) -1 ) / x )
  85. * where PPP(X), x and y are positive
  86. */
  87.  
  88. class WPSE_Inject
  89. {
  90. protected $results = NULL;
  91. protected $query = NULL;
  92. protected $nr = 0;
  93. protected $inject_mode = FALSE;
  94. protected $args = array();
  95.  
  96. public function __construct( $args = array() )
  97. {
  98. $defaults = array(
  99. 'items_before_each_inject' => 5,
  100. 'cpt_items_per_inject' => 1,
  101. 'cpt' => 'post',
  102. 'paged' => 1,
  103. 'template_part' => 'content-post'
  104. );
  105. $this->args = wp_parse_args( $args, $defaults );
  106. }
  107.  
  108. public function init()
  109. {
  110. add_action( 'loop_start', array( $this, 'loop_start' ) );
  111. add_action( 'loop_end', array( $this, 'loop_end' ) );
  112. }
  113.  
  114. public function cpt_items_on_this_page( WP_Query $query )
  115. {
  116. $count = $this->args['cpt_items_per_inject'] * floor( ( $query->get( 'posts_per_page' ) -1 ) / $this->args['items_before_each_inject'] );
  117. return ( $count > 0 ) ? $count : 1;
  118. }
  119.  
  120. public function loop_start( WP_Query $query )
  121. {
  122. $this->query = $query;
  123.  
  124. if( $query->is_main_query() )
  125. {
  126. $args = array(
  127. 'post_type' => $this->args['cpt'],
  128. 'posts_per_page' => $this->cpt_items_on_this_page( $query ),
  129. 'paged' => $this->args['paged'],
  130. 'suppress_filters' => TRUE,
  131. );
  132. $this->results = new WP_Query( $args );
  133. add_action( 'the_post', array( $this, 'the_post' ) );
  134. }
  135. }
  136.  
  137. public function loop_end( WP_Query $query )
  138. {
  139. if( $query->is_main_query() )
  140. remove_action( 'the_post', array( $this, 'the_post' ) );
  141. }
  142.  
  143. public function the_post()
  144. {
  145. if( ! $this->inject_mode
  146. && 0 < $this->nr
  147. && 0 === $this->nr % $this->args['items_before_each_inject'] )
  148. {
  149. $this->inject_mode = TRUE;
  150. $this->results->rewind_posts();
  151. $this->results->current_post = ( absint( $this->nr / $this->args['items_before_each_inject'] ) -1 ) * $this->args['cpt_items_per_inject'] - 1;
  152. $j = 1;
  153. if ( $this->results->have_posts() ) :
  154. while ( $this->results->have_posts() ) :
  155. $this->results->the_post();
  156. get_template_part( $this->args['template_part'] );
  157. if( $this->args['cpt_items_per_inject'] < ++$j )
  158. break;
  159.  
  160. endwhile;
  161. wp_reset_postdata();
  162. endif;
  163. $this->inject_mode = FALSE;
  164. }
  165.  
  166. if( ! $this->inject_mode )
  167. $this->nr++;
  168.  
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement