Display a search category and tag name in WordPress

November 15, 2024
No Comments
1 min read

To display a search category name in WordPress, you typically want to check if the current query is for a specific category and then retrieve its name. Here's how you can achieve this:

1. For a Category Archive Page

If you're on a category archive page, you can use the single_cat_title() function to display the category name.

PHP
<?php
if (is_category()) {
    single_cat_title();
}
?>

2. For Search Results with a Specific Category

If you have a search query that filters by a category (e.g., via a dropdown or URL parameter), you can retrieve the category name using the get_category_by_slug() or get_term() function.

Here’s an example assuming the category is passed via a cat parameter in the query:

PHP
<?php
if (is_search() && isset($_GET['cat']) && !empty($_GET['cat'])) {
    $category_id = intval($_GET['cat']);
    $category = get_category($category_id);

    if ($category) {
        echo esc_html($category->name);
    } else {
        echo "Category not found.";
    }
}
?>

3. Custom Taxonomies

If you're working with a custom taxonomy instead of categories, you can use a similar approach:

PHP
<?php
if (is_tax('your_custom_taxonomy')) {
    single_term_title();
}
?>

4. In a Search Form with Categories

If you want to show the category name dynamically in a dropdown, you can populate it using wp_dropdown_categories():

PHP
<?php
wp_dropdown_categories([
    'show_option_all' => 'All Categories',
    'name' => 'cat',
    'selected' => get_query_var('cat'),
]);
?>

Notes:

  • Sanitize Output: Always sanitize the output using esc_html() or similar functions.
  • Search and Categories Together: To filter search results by category, ensure your search form includes a category field.
  • Category Parameter: Check your URL parameters to confirm you're filtering correctly.

Display the tag name

To display the tag name in WordPress, you can use the following approaches based on your context:


1. For a Tag Archive Page

If you are on a tag archive page, you can use single_tag_title() to display the current tag name:

PHP
<?php
if (is_tag()) {
    single_tag_title();
}
?>

2. For Search Results with a Specific Tag

If you're using a search form or URL to filter by tag (e.g., a tag query parameter), you can retrieve the tag name using get_term_by().

Here’s an example:

PHP
<?php
if (is_search() && isset($_GET['tag']) && !empty($_GET['tag'])) {
    $tag_slug = sanitize_text_field($_GET['tag']);
    $tag = get_term_by('slug', $tag_slug, 'post_tag');

    if ($tag) {
        echo esc_html($tag->name);
    } else {
        echo "Tag not found.";
    }
}
?>

3. For a Custom Taxonomy with Tags

If you are working with a custom taxonomy instead of the default post_tag, use the following:

PHP
<?php
if (is_tax('your_custom_taxonomy')) {
    single_term_title();
}
?>

Replace your_custom_taxonomy with the slug of your taxonomy.


4. In a Search Form with Tags

If you want to show the tag name dynamically in a dropdown, you can use wp_tag_cloud() or a custom query:

Using wp_tag_cloud:

PHP
<?php
wp_tag_cloud([
    'taxonomy' => 'post_tag',
    'format'   => 'flat', // or 'list'
    'smallest' => 8,
    'largest'  => 22,
]);
?>

Using a Custom Dropdown:

PHP
<?php
$tags = get_terms([
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
]);

if (!empty($tags) && !is_wp_error($tags)) {
    echo '<select name="tag">';
    echo '<option value="">All Tags</option>';
    foreach ($tags as $tag) {
        echo '<option value="' . esc_attr($tag->slug) . '">' . esc_html($tag->name) . '</option>';
    }
    echo '</select>';
}
?>

Notes:

  • Sanitize Output: Always use esc_html() for output and sanitize input using sanitize_text_field() or similar.
  • URL Structure: Ensure your tag filtering uses the tag query parameter, or adapt the code to your setup.
©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.