我正在使用(并且喜欢)WordPress的Relevanssi插件。虽然在用户搜索中记录两次搜索时遇到问题。
在插件评论中找到此评论,说明它是模板的问题。
在挖掘我的搜索模板时,我只是删除了所有内容,然后零碎地添加内容,直到我再次获得双倍结果。
这是我的搜索模板中的一行,似乎是麻烦制造者,但我不一定明白为什么。
<div style="background-image: url();"></div>
这对我来说是0有意义的。如果那位 STATIC html 在 content-search.php
模板中,我会记录双重搜索。如果我删除它,或者所有文章都有get_the_post_thumbnail_url
,那么我只会记录一个搜索。
这似乎与style="background-image:url();"
没有价值有关?
这是更大的图景:
搜索.php:
<?php
global $wp_query;
if ( $wp_query->found_posts > 12 ) {
$search_count = '1 - ' . $wp_query->post_count . ' of ' . $wp_query->found_posts;
} else {
$search_count = $wp_query->post_count;
}
?>
<div class="search-pg-header">
<?php include get_template_directory() . '/templates/partials/search-form.php'; ?>
<?php if ( get_search_query() !== '' ) { ?>
<h1>Showing <?php echo $search_count; ?> results for <?php echo get_search_query() ?></h1>
<?php } // end if ?>
</div>
<div class="posts-container">
<?php if ( have_posts() ):
while ( have_posts() ) : the_post();
get_template_part( 'templates/content', 'search' );
endwhile;
else: ?>
<p>There are no articles that matched your search.</p>
<?php endif; ?>
</div>
<?php if ( $wp_query->max_num_pages > 1 ) { ?>
<button>Show More</button>
<?php } // end if ?>
内容搜索.php:
<article data-link="<?php the_permalink(); ?>">
<div style="background-image: url(<?php the_post_thumbnail_url('card-white');?>);"></div>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
<?php if ( has_excerpt() ) {?>
<p><?php echo get_the_excerpt(); ?></p>
<?php } //end if ?>
</div>
</article>
我重新安装了Wordpress,并以2017年为主题测试了这个奇怪的理论。确实如此,如果我template-parts/post/content-excerpt.php
模板中添加<div style="background-image:url();"></div>
,则会记录重复的搜索。
如果该div 有图像:<div style="background-image:url(http://image.com);"></div>
只记录一个搜索。
正如Mikko Saari(Relevanssi)如此友善地帮助我一样,这实际上是浏览器的意外行为。
至少对此有一个解释绝对是件好事。
在将内联样式渲染到页面之前,我只需检查是否有 bkgd 图像即可防止这种情况,现在我知道将来要更加小心这种事情!
$style = ! empty( get_the_post_thumbnail_url($the_post, 'card-white') ) ?
'style="background-image: url(' . get_the_post_thumbnail_url($the_post, 'card-white') . ');"' : '';
<div class="article-card__img" <?php echo $style; ?>></div>