php - Wordpress loops efficiency -
i build site wordpress: http://mida.org.il/
as can see, homepage takes lot of time load. i'm trying fix - there 5 custom loops in page, 3 of them using posts_per_page
, cat
query posts, , posts_per_page
set 3. question is, if loop gets third post, stops , and breaks out, or keep looping until gets last post? if second correct, no wonder it's slow, site holds thousands of posts.
the code loops:
if ( $first_special_cat ){ $args = array( 'cat'=>$first_special_cat, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky ); $cat_name = $first_special_cat; $cat_id = get_cat_id($first_special_cat); }else{ $args = array( 'cat'=>50, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky ); $cat_name = get_cat_name(50); $cat_id = 50; } $the_query = new wp_query($args); echo '<div class="special-proj-main-title">'; echo '<div class="homepage-blueline-title"></div>'; echo '<h4 class="special-cat-name"><a href="' . esc_url( get_term_link($cat_id) ) . '">' . $cat_name . '</h4>'; echo '</div>'; ?> <div class="row"> <div class="col-sm-4"> <?php if ( $the_query->have_posts() ): ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); //setting 3 posts right: ?> <h2 class="special-project-title"><a class="special-proj-title-link" href="<?php echo esc_url( get_the_permalink() )?>"><?php the_title()?></a></h2> <br/> <div class="post-meta special-project-meta"><?php mida_post_meta()?></div><br/> <?php endwhile; wp_reset_postdata(); ?> <span class="to-all-posts"><a href="<?php echo esc_url( get_term_link($cat_id) )?>"><?php echo sprintf( __('load more posts %s', 'mida'), $cat_name ); ?></a></span> <?php else: echo "you put wrong id"; endif; ?> </div> <div class="col-sm-8 home-background-img"> <?php if ( $first_special_post ) $args = array('name' => $first_special_post, 'posts_per_page' => 1 ); else $args = array('cat'=>50, 'posts_per_page' => 1, 'orderby'=>'date' ); $the_query = new wp_query( $args ); if ( $the_query->have_posts() ): while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php $first_special_img = get_field('rectangular_image'); if ( $first_special_img ) $first_special_img_src = wp_get_attachment_image_src( $first_special_img['id'], 'full' ); else $first_special_img_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); ?> <div class="special-project-section" style="background: url('<?php echo $first_special_img_src[0]; ?>');background-size: contain;"> <a href="<?php echo esc_url( get_the_permalink() )?>" title="<?php the_title() ?>"><span style=" position:absolute; width:100%; height:100%; top:0; left:0; z-index: 1;"></</span> </a> <?php echo '<div class="special-cat-on-img">'; echo $first_special_text ? '<h5><div class="special-cat-name-img">' . $first_special_text . '</div></h5>' : '<h5><div class="special-cat-name-img">' . __('special project', 'mida') . '</div></h5>'; ?> <h6 class="speical-cat-title-img"><a href="<?php the_permalink()?>"> <?php the_title() ?> </a></h6> <?php echo '</div>'; ?> <div class="blue-line"><?php echo '<div class="special-proj-ex">' . $first_special_cat_ex . '</div>'; ?></div> </div> <?php endwhile; wp_reset_postdata(); endif; ?> </div> </div>
x3.
every loop query different categories ( $first_special_
custom fields, input user ).
so can me optimize code (and answer above question)?
thanks!
you problem not in loop rather in page itself. total page size whopping 11.8mb! looks due ton of images. might try little image optimization (use jpg's post thumbs/images) , make sure images sized correctly. honestly, lazy loading might solution here! there plenty of options out there make easy implement.
Comments
Post a Comment