这段代码是自定义WordPress主页的一部分,该主页显示了使用WP\\U查询的7篇最新帖子,第一篇帖子有一个自定义html结构,而其余的则有另一个html结构。
然后还有第二个WP\\u查询,该查询查询最近5篇文章中的特定标记,如果一篇文章有相同的标记,并且已经出现第一个WP\\u查询,则不会显示任何重复。然后,代码还在第一篇文章中使用自定义html,其余部分的格式不同。
多年来,代码一直工作正常,但最近加载页面需要很长时间(大约7秒),使用WordPress中的查询监视器插件,WP\\U查询显示为非常慢的查询。
因此,如何优化或重写WP\\u查询和“不要重复代码”,使其更高效、更快,以便页面的每个块在过滤重复帖子的同时显示帖子。
<section class="news-front">
</div>
<div class="news-list-col-1">
<?php $count = 0; //Count starts to edit each post ?>
<?php $do_not_duplicate = array(); ?>
<?php $query = new WP_Query(array (
\'posts_per_page\' => 7,
\'no_found_rows\' => true,
)
);?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php $do_not_duplicate[] = $post->ID; ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<article class="news-item-1" <?php post_class();?> >
<a href="<?php the_permalink() ?>" class="cover-link">
<?php if(has_post_thumbnail($post->ID)) :?><!-- .article_image -->
<div id="post-hero-image" style="background-image: url(\'<?php the_post_thumbnail_url(\'large\'); ?>\')" ></div><!-- .article_image -->
<?php else :?>
<!-- Nothing inside -->
<?php endif;?><!-- .article_image -->
<header class="news-item-data">
<h2 itemprop="headline" class="entry-title"><?php the_title(); ?></h2>
<?php get_template_part( \'types/content\', \'meta\' ); ?><!-- .entry-meta -->
<?php if( $post->post_excerpt ) : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php echo $post->post_excerpt; ?></div>
</div>
<?php else : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php custom_excerpt(154); ?></div>
</div>
<?php endif; ?>
</header>
</a>
</article>
</div>
<div class="news-list-col">
<?php else : ?>
<article class="news-items-list" <?php post_class();?> >
<a href="<?php the_permalink() ?>" class="cover-link" >
<?php if(has_post_thumbnail($post->ID)) :?><!-- .article_image -->
<div id="post-hero-image" style="background-image: url(\'<?php the_post_thumbnail_url(\'medium_large\'); ?>\')" ></div><!-- .article_image -->
<?php else :?>
<!-- Nothing inside -->
<?php endif;?><!-- .article_image -->
<header class="news-item-data">
<h2 itemprop="headline" class="entry-title"><?php the_title(); ?></h2>
<?php get_template_part( \'types/content\', \'meta\' ); ?><!-- .entry-meta -->
</header>
<?php if ( !wp_is_mobile() ) :?>
<?php if( $post->post_excerpt ) : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php echo $post->post_excerpt; ?></div>
</div>
<?php else : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php custom_excerpt(154); ?></div>
</div>
<?php endif; ?>
<?php endif; ?>
</a>
</article>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</section>
<section class="news-grid-5">
<div class="news-list-col-1">
<?php $count = 0; //Count starts to edit each post ?>
<?php $query = new WP_Query(array(
\'tag_slug__in\' => array(\'how-to\'),
\'posts_per_page\' => 5,
\'order\' => \'DESC\',
\'no_found_rows\' => true,
\'post__not_in\' => $do_not_duplicate,
)
);?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php $do_not_duplicate[] = $post->ID; ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<article class="news-grid-5-item-1" >
<a href="<?php the_permalink() ?>" class="cover-link" >
<?php if(has_post_thumbnail($post->ID)) :?><!-- .article_image -->
<div id="post-hero-image" style="background-image: url(\'<?php the_post_thumbnail_url(\'large\'); ?>\')" ></div><!-- .article_image -->
<?php else :?>
<!-- Nothing inside -->
<?php endif;?><!-- .article_image -->
<header class="news-item-data">
<h2 itemprop="headline" class="entry-title"><?php the_title(); ?></h2>
<?php get_template_part( \'types/content\', \'meta\' ); ?><!-- .entry-meta -->
<?php if( $post->post_excerpt ) : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php echo $post->post_excerpt; ?></div>
</div>
<?php else : ?>
<div class="post-description" >
<div class="post-descr-content" ><?php custom_excerpt(154); ?></div>
</div>
<?php endif; ?>
</header>
</a>
</article>
<?php else : ?>
<article class="news-grid-1" >
<a href="<?php the_permalink() ?>" class="cover-link" >
<?php if(has_post_thumbnail($post->ID)) :?><!-- .article_image -->
<div id="post-hero-image" style="background-image: url(\'<?php the_post_thumbnail_url(\'large\'); ?>\')" ></div><!-- .article_image -->
<?php else :?>
<!-- Nothing inside -->
<?php endif;?><!-- .article_image -->
<header class="news-item-data">
<h2 itemprop="headline" class="entry-title"><?php the_title(); ?></h2>
<?php get_template_part( \'types/content\', \'meta\' ); ?><!-- .entry-meta -->
</header>
</a>
</article>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</section>