mirai-mamori / Sakurairo

一个多彩、轻松上手、体验完善,具有强大自定义功能的WordPress主题(基于Sakura主题,支持简繁英日多语言)A Colorful, Easy-to-use, Perfect Experience, and Powerful Customizable WordPress Theme (Based on Theme Sakura. Support Simplified Chinese, Traditional Chinese, English and Japanese languages)
https://docs.fuukei.org
GNU General Public License v2.0
2.95k stars 354 forks source link

关闭搜索页面结果 #881

Closed zachb3123 closed 3 months ago

zachb3123 commented 4 months ago

现有内容的优化 / 新增功能 / UI 界面 / 用户体验:

每当搜索到文章的标签分类很正常,如果用户搜索到页面标题会导致“未分类”的出现。在搜索网站内容的时候能不能不搜索页面的标题名字的结果?

请看下面的图片: image 例子1

image 例子2

github-actions[bot] commented 4 months ago

@zachb3123,感谢你给 sakurairo 提出了 issue。ヾ(≧▽≦*)o 请根据 issue 模版确保背景信息的完善,我们将调查并尽快回复你。

kanodaisuki commented 4 months ago

看到以前提交过一个建议#786,和这个类似。但我还是无法理解,请在此问题下详细描述你的想法和建议,我们可以讨论一下。

zachb3123 commented 4 months ago

简单来说那就是不用搜索页面板块,只搜文章标题和文章的内容就行。页面直接点击就行, 如果有很多的用户使用页面,可以加入搜索搜索筛选

本来页面就不会有标签的,只有文章有标签,所以会有未分类显示 image

这也有可能主题的显示的原因, 因为点击主题每一个页面、文章、标签都是以同一方式显示的.

下面是我简单修改一下:

①例如把这些加入 functions.php 文件:

// Modify search query to exclude exact page name results
function exclude_page_from_search($query) {
    if (!is_admin() && $query->is_search) {
        $query->set('post_type', 'post'); // Only search posts
    }
    return $query;
}
add_filter('pre_get_posts','exclude_page_from_search');

②又或者修改search.php显示方式:

<?php
/**
 * The template for displaying search results pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package Akina
 */

get_header(); ?>

<section id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php if (have_posts()) : ?>
            <header class="page-header">
                <h1 class="page-title"><?php printf(__('Search Results for: %s', 'sakurairo'), '<span>' . get_search_query() . '</span>'); ?></h1>
            </header><!-- .page-header -->

            <?php
            /* Start the Loop */
            while (have_posts()) : the_post();
                // Exclude pages from search results
                if ('page' === get_post_type()) {
                    continue;
                }

                // Include template part for posts
                get_template_part('tpl/content', get_post_format());
            endwhile;

            the_posts_navigation();

        else : ?>
            <div class="search-box">
                <!-- search start -->
                <form class="s-search">
                    <input class="text-input" type="search" name="s" placeholder="<?php _e('Search...', 'sakurairo') ?>" required>
                </form>
                <!-- search end -->
            </div>
        <?php
            get_template_part('tpl/content', 'none');

        endif; ?>

        <style>
            .nav-previous,
            .nav-next {
                padding: 20px 0;
                text-align: center;
                margin: 40px 0 80px;
                display: inline-block;
                font-family: 'Fira Code', 'Noto Sans SC';
            }

            .nav-previous,
            .nav-next a {
                padding: 13px 35px;
                border: 1px solid #D6D6D6;
                border-radius: 50px;
                color: #ADADAD;
            }

            .nav-previous,
            .nav-next span {
                color: #989898;
                font-size: 15px;
            }

            .nav-previous,
            .nav-next a:hover {
                border: 1px solid #A0DAD0;
                color: #A0DAD0;
            }
        </style>

    </main><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();
kanodaisuki commented 3 months ago

简单来说那就是不用搜索页面板块,只搜文章标题和文章的内容就行。页面直接点击就行, 如果有很多的用户使用页面,可以加入搜索搜索筛选

可以提交pr至preview分支@zachb3123