标签: 搜索模板

  • 给wordpress不同分类添加不同的搜索结果列表页模板

    在WordPress中,为不同的分类添加不同的搜索结果列表页模板,可以通过自定义分类模板和搜索结果模板来实现。以下是详细的步骤:

    1. 创建自定义分类模板

    首先,你需要为每个分类创建一个自定义的分类模板。假设你有两个分类:`category-news` 和 `category-blogs`。

    #创建分类模板文件

    1. 在你的主题目录下创建一个名为 `category-news.php` 的文件。

    2. 在该文件中添加以下代码:

    <?php
    /**
     * Template Name: Category News Template
     */
    get_header(); ?>
    
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
    
            <?php if ( have_posts() ) : ?>
    
                <header class="page-header">
                    <h1 class="page-title"><?php single_cat_title(); ?></h1>
                </header>
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php get_template_part( 'content', get_post_type() ); ?>
    
                <?php endwhile; ?>
    
                <?php the_posts_navigation(); ?>
    
            <?php else : ?>
    
                <?php get_template_part( 'content', 'none' ); ?>
    
            <?php endif; ?>
    
        </main><!-- #main -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    3. 创建另一个名为 `category-blogs.php` 的文件,并添加类似的代码:

    <?php
    /**
     * Template Name: Category Blogs Template
     */
    get_header(); ?>
    
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
    
            <?php if ( have_posts() ) : ?>
    
                <header class="page-header">
                    <h1 class="page-title"><?php single_cat_title(); ?></h1>
                </header>
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php get_template_part( 'content', get_post_type() ); ?>
    
                <?php endwhile; ?>
    
                <?php the_posts_navigation(); ?>
    
            <?php else : ?>
    
                <?php get_template_part( 'content', 'none' ); ?>
    
            <?php endif; ?>
    
        </main><!-- #main -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    2. 创建自定义搜索结果模板

    接下来,你需要创建一个自定义的搜索结果模板,并在其中根据分类显示不同的内容。

    #创建搜索结果模板文件

    1. 在你的主题目录下创建一个名为 `search.php` 的文件。

    2. 在该文件中添加以下代码:

    <?php
    /**
     * Template Name: Custom Search Template
     */
    get_header(); ?>
    
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
    
            <?php if ( have_posts() ) : ?>
    
                <header class="page-header">
                    <h1 class="page-title"><?php echo esc_html_x( 'Search Results for: ', 'label', 'text-domain' ); ?><?php echo get_search_query(); ?></h1>
                </header>
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php get_template_part( 'content', get_post_type() ); ?>
    
                <?php endwhile; ?>
    
                <?php the_posts_navigation(); ?>
    
            <?php else : ?>
    
                <?php get_template_part( 'content', 'none' ); ?>
    
            <?php endif; ?>
    
        </main><!-- #main -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    3. 根据分类显示不同的搜索结果模板

    为了根据分类显示不同的搜索结果模板,你可以在 `search.php` 中添加条件判断。

    <?php
    /**
     * Template Name: Custom Search Template
     */
    get_header(); ?>
    
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
    
            <?php if ( have_posts() ) : ?>
    
                <header class="page-header">
                    <h1 class="page-title"><?php echo esc_html_x( 'Search Results for: ', 'label', 'text-domain' ); ?><?php echo get_search_query(); ?></h1>
                </header>
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php $categories = get_the_category(); ?>
                    <?php if ( in_array( 'news', wp_list_pluck( $categories, 'slug' ) ) ) : ?>
                        <?php include( get_template_directory() . '/category-news.php' ); ?>
                    <?php elseif ( in_array( 'blogs', wp_list_pluck( $categories, 'slug' ) ) ) : ?>
                        <?php include( get_template_directory() . '/category-blogs.php' ); ?>
                    <?php else : ?>
                        <?php get_template_part( 'content', get_post_type() ); ?>
                    <?php endif; ?>
    
                <?php endwhile; ?>
    
                <?php the_posts_navigation(); ?>
    
            <?php else : ?>
    
                <?php get_template_part( 'content', 'none' ); ?>
    
            <?php endif; ?>
    
        </main><!-- #main -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    通过以上方法,你可以为不同的分类添加不同的搜索结果列表页模板。这样,当用户在搜索框中输入关键词时,WordPress会根据文章的分类显示相应的自定义模板。