0

Is it possible to return a specific tag archive/template as one of the search results?

For example, let's say my site has the tag:

  • Summer

And my site has the pages:

  • Summer picnicking
  • Fun, fun, fun (Has the tag 'Summer' on it)
  • Sample page (Has the word 'Summer' in the content of the page)

And let's say that the user searches "Summer".

Normally, the three pages would return because they either have 'Summer' in the title, in the content, or the tag 'Summer' applied to them.

But I'd like there to be a fourth returned result: the archive for the tag "Summer". So that the four returned results are:

  1. Summer (tag archive)
  2. Summer picnicking
  3. Fun, fun, fun
  4. Sample page

Is there a way to do this?

0

A simple way to do is to edit your theme's "search.php" (copying it from the parent theme into your child theme's directory first if you're using a child theme) and then before the posts loop putting something like:

        <?php
            if ( $tag = get_term_by( 'name', get_search_query(), 'post_tag' ) ) {
            ?>
                <article><!-- or whatever is consistent with rest of listing. -->
                    <h2 class="entry-title"><!-- ditto -->
                        <a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" rel="bookmark"><?php printf( __( '%s (tag archive)' ),  $tag->name ); ?></a>
                    </h2>
                </article>
            <?php
            }
        ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.