简体   繁体   English

WordPress循环效率

[英]Wordpress loops efficiency

I build this site with wordpress: http://mida.org.il/ 我使用wordpress建立此网站: http : //mida.org.il/

As you can see, homepage takes a lot of time to load. 如您所见,首页加载需要花费很多时间。 I'm trying to fix this - there are five custom loops in that page, three of them using posts_per_page and cat to query posts, and posts_per_page set to 3. My question is, if the loop gets to the third post, it stops and and breaks out, or its keep looping until it gets to the last post? 我正在尝试解决此问题-该页面中有五个自定义循环,其中三个使用posts_per_pagecat查询帖子,而posts_per_page设置为3。我的问题是,如果该循环到达第三篇文章,它将停止并并爆发,或者一直循环直到到达最后一个帖子? If the second is correct, no wonder that it's so slow, this site holds thousands of posts. 如果第二个是正确的,也难怪它太慢了,此站点将保存数千个帖子。

The code for the 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 the three posts to the 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 from %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. X3

every loop query different categories ( $first_special_ are custom fields, input from the user ). 每个循环查询不同的类别( $first_special_是自定义字段,由用户输入)。

So can anyone help me optimize this code (and answer the above question)? 那么有人可以帮助我优化此代码(并回答上述问题)吗?

Thanks! 谢谢!

You problem is NOT in your loop but rather in the page itself. 您的问题不在循环中,而是在页面本身中。 Total page size is a whopping 11.8MB! 页面总大小高达11.8MB! This looks primarily due to a ton of images. 这主要归因于大量图像。 You might try a little image optimization (use jpg's for post thumbs/images) and make sure images are sized correctly. 您可以尝试进行一些图像优化(将jpg用作后置拇指/图像),并确保图像尺寸正确。 Honestly, lazy loading might be a good solution here! 老实说,延迟加载可能是一个不错的解决方案! There are plenty of options out there to make it easy to implement. 有很多选择可以使它易于实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM