【问题标题】:Exclude image paths from Search result从搜索结果中排除图像路径
【发布时间】:2018-04-19 23:44:08
【问题描述】:

我创建了一个 wordpress 网站。它具有 Wordpress 内置的标准搜索功能。搜索工具有一个小(但主要)问题。它在页面内容中查找关键字。

例如网站网址是:www.mywebsite.com

在内容中添加了一张图片,里面有一些段落,它的html代码是这样的:

<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Laborum facere porro aut suscipit nostrum nisi, illo excepturi possimus quibusdam obcaecati blanditiis ex, quos necessitatibus officia recusandae totam aliquid quia. Quasi.</p>

<img src="http://www.mywebsite.com/wp-content/themes/mytheme/img/photo.jpg" alt="">

<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eos consectetur pariatur amet tempore at suscipit, tenetur modi fugiat perspiciatis quaerat distinctio dolor recusandae aliquam consequatur ullam doloremque voluptas quos eaque.</p>

因此,如果我搜索关键字 mywebsite,它会在搜索结果中返回此页面,因为它会在下面的图像路径中找到关键字 mywebsite

www.mywebsite.com/wp-content/themes/mytheme/img/photo.jpg

实际上这是错误的,因为实际页面内容文本中没有关键字 mywebsite

我该如何解决。这里有两个选项:

  1. 想办法在搜索结果中排除图片路径
  2. 排除用于搜索查找的 page_content。因此它会查找帖子标题和其他字段,但不会查找内容。

【问题讨论】:

  • 你有想过这个吗?

标签: wordpress search


【解决方案1】:

这个应该比较简单,试试这个:

// Exclude images from search results - WordPress
add_action( 'init', 'exclude_images_from_search_results' );
function exclude_images_from_search_results() {
    global $wp_post_types;

    $wp_post_types['attachment']->exclude_from_search = true;
}

在您的functions.php 文件中添加以下sn-p。假设图像是上述帖子的一部分,这应该将它们排除在外。希望这会有所帮助!

编辑:

我会在你的位置做的是修改搜索结果模板,或创建你自己的。您可以修改之前在搜索结果模板中提到的内容。我创建了一个示例,它会提取您内容中的所有图像标签,并输出您的标题以及经过清理的内容(在这种情况下,没有图像)。

<?php get_header(); ?>
<section id="primary" class="content-area">
    <main id="main" class="site-main">

    <?php if ( have_posts() ) : ?>

        <header class="page-header">
            <h1 class="page-title">
                <?php
                /* translators: %s: search query. */
                printf( esc_html__( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' );
                ?>
            </h1>
        </header><!-- .page-header -->

        <?php
        /* Start the Loop */
        while ( have_posts() ) :

            the_post();

            $post_title = $post->post_title;
            $post_content = $post->post_content;
            $post_content = preg_replace("/<img[^>]+\>/i", " ", $post_content);          
            $post_content = apply_filters('the_content', $post_content);
            $post_content = str_replace(']]>', ']]>', $post_content);


            echo "<article>";
                echo "<h1>" . $post_title . "</h1>";
                echo "<p>" . $post_content . "</p>";
            echo "</article>";

        endwhile;

        the_posts_navigation();

    else :

        // This is where your no search results output block would be
        get_template_part( 'template-parts/content', 'none' );

    endif;
    ?>

    </main><!-- #main -->
</section><!-- #primary -->
<?php
get_sidebar();
get_footer();

【讨论】:

  • 不,它不起作用。您的代码所做的是,它不包括具有帖子类型“附件”的帖子。它不会查看帖子内容,但会查找帖子本身的类型。
  • 您是否尝试过编辑或创建自己的搜索结果模板?我已经用一个示例更新了解决方案,该示例通过清理帖子内容来删除所有图像标签。见上文。
  • 我的问题是关于不显示由于图像路径而出现的搜索结果。
猜你喜欢
  • 1970-01-01
  • 2020-12-12
  • 2023-03-24
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 2021-12-14
  • 2014-01-28
  • 1970-01-01
相关资源
最近更新 更多