1

我正在使用(并且喜欢)用于 WordPress 的 Relevanssi 插件。尽管在用户搜索中记录了两次搜索,但存在问题。

在插件评论中发现这条评论是模板的问题

在搜索我的搜索模板时,我只是删除了所有内容,然后零碎地添加内容,直到再次获得双重结果。

这是我的搜索模板中似乎是麻烦制造者的行,但我不一定明白为什么。

<div style="background-image: url();"></div>

这对我来说是 0 意义。如果那位 STATIC html 在content-search.php模板中,我会记录双重搜索。如果我删除它,或者如果所有文章都有get_the_post_thumbnail_urls,那么我只会记录一次搜索。

这似乎与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>
4

1 回答 1

1

我重新安装了 Wordpress,并用 2017 年的主题测试了这个奇怪的理论。确实如此,如果我添加<div style="background-image:url();"></div>到模板template-parts/post/content-excerpt.php中,则会记录重复搜索。

如果该 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>
于 2017-11-02T14:31:20.937 回答