Support » Developing with WordPress » How do I show 2 or more categories as a h1 title in category.php in WordPress

  • How do I show 2 or more categories as a h1 title in category.php in WordPress after clicking a link with a plus mark? domain/category/X+Y/

    When I click on a link for example I get the first category “beaches” in the title but how do I get 2 keywords or more.

    <?php single_cat_title(''); ?>
    <?php single_cat_title('Category: '); ?>

    single_cat_title > multiple_cat_title
    I have custom posts with custom categories (custom taxonomy).

    Now I want to create a page (category.php) where posts from two categories are listed in the title H1 area.

    The standard function used to list the current category is:

    <?php echo single_cat_title(); ?>
    This works great but only when the page is:

    When the domain is: the category is only “category-name”.

    When I use:

    <?php wp_list_categories(); ?>
    All categories that has posts are listed but I can’t find any code to just list all the categories in the category.php file h1 title.

    Is there a function that would do just:

    multiple_cat_title(); Some extra info: The custom taxonomies are: museums and free. I want to show “Free + Museums (26)” when people visit the URL:


    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    You’ll need some custom coding on the template file being used. Use the “single_cat_title” filter to append the second category to the default title if you want them both on one line (or two) in one H1 block. FWIW, it’s considered poor SEO to have more than one H1 element on a page.

    You can get the second category slug from the global $wp_query query vars (keyed “category_name”). The value will be something like “free+museums”, so you’d explode() the string on “+” to break up the string into an array, the second element (index=1) will be “museums”. If you want the category name instead of its slug, you’ll need to get the term object with get_term_by(), which will accept a slug argument. The term object will have the category name as its “name” property.

    If you add the filter callback right on the template just before calling single_cat_title(), your callback can then remove itself from the filter stack when it’s done so you needn’t worry about the callback influencing other uses of the function anywhere else.

    Thread Starter a4jp . com


    I want to show it like this <h1>Free + Museums</h1> or if that is difficult just have <h1>free+museums</h1> if possible. I searched Google and a lot of sites but couldn’t find any samples of working code anywhere. Maybe I’m just searching for the wrong keywords.

    The rules on multiple h1 tags have changed. Now Google is okay with multiple tags. Some of the top ranking sites are doing this now but I’m okay with just one on my site. I’m more interested in a functional site than spending heaps of time trying to get the SEO perfect.

    Moderator bcworkz


    The query var “category_name” will have a value like “free+museums” or whatever the term slugs are. You could use standard string functions to capitalize and add spaces, but that’s assuming the display name is only a capitalized version of the slug. Slugs could be very different from the display name. To get the proper display name you need to get the associated WP_Term object. There is much room for improvement, but basic code to build upon would be something like this (untested):

    add_filter('single_cat_title', 'multi_cat_title');
    function multi_cat_title( $name ) {
      global $wp_query;
      if ( strpos( $wp_query->get('category_name', '+') !== false )) {
        $name = $wp_query->get('category_name');
        // could use ucwords() and str_replace() to further modify $name here
      return $name;
    Thread Starter a4jp . com


    I gave it a try in the template file and in the functions file but it just made the website white.

    Thread Starter a4jp . com


    Your file goes into the functions.php file, doesn’t it?

    Moderator bcworkz


    functions.php is one option. It should be fine there since it checks for the presence of “+” before doing anything. The other option is as described in the last paragraph of my initial reply. That’ll work too. It’s the better option if it’s difficult to distinguish which query to operate on. But checking for “+” should be pretty reliable.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.