Automatically display titles on Category and Tag Archives

The archive pages in Genesis can be a bit confusing. Other than the URL (and breadcrumbs if you have them turned on), they look exactly the same as the main blog.

Genesis provides a useful feature for adding headlines and intro text to the top of your archive pages. Go to Posts > Categories and edit a category to see the screenshot above. But this requires you to manually type it in for each category. If you have a lot of categories this can be a hassle.

Add this code to your theme’s functions.php to automatically default that field to your category name. If you provide an “Archive Headline” when editing a category, that is used. But if you don’t, the category name is used. This applies to categories, tags, and any custom taxonomies you’ve created.

<?php
/**
* Default Titles for Term Archives
*
* @author Bill Erickson
* @see http://www.billerickson.net/default-category-and-tag-titles
*
* @param string $value
* @param int $term_id
* @param string $meta_key
* @param bool $single
* @return string $vlaue
*/
function ea_default_term_title( $value, $term_id, $meta_key, $single ) {
if( ( is_category() || is_tag() || is_tax() ) && 'headline' == $meta_key && ! is_admin() ) {
// Grab the current value, be sure to remove and re-add the hook to avoid infinite loops
remove_action( 'get_term_metadata', 'ea_default_term_title', 10 );
$value = get_term_meta( $term_id, 'headline', true );
add_action( 'get_term_metadata', 'ea_default_term_title', 10, 4 );
// Use term name if empty
if( empty( $value ) ) {
$term = get_queried_object();
$value = $term->name;
}
}
return $value;
}
add_filter( 'get_term_metadata', 'ea_default_term_title', 10, 4 );
view raw functions.php hosted with ❤ by GitHub

Thanks Joshua Nelson for helping update this code to work with the latest version of Genesis.

And here’s a similar version of the code that uses the term’s description for the “intro text” if that’s empty:

<?php
/**
* Default Descriptions for Term Archives
*
* @author Bill Erickson
* @see http://www.billerickson.net/default-category-and-tag-titles
*
* @param string $value
* @param int $term_id
* @param string $meta_key
* @param bool $single
* @return string $vlaue
*/
function ea_default_term_description( $value, $term_id, $meta_key, $single ) {
if( ( is_category() || is_tag() || is_tax() ) && 'intro_text' == $meta_key && ! is_admin() ) {
// Grab the current value, be sure to remove and re-add the hook to avoid infinite loops
remove_action( 'get_term_metadata', 'ea_default_term_description', 10 );
$value = get_term_meta( $term_id, 'intro_text', true );
add_action( 'get_term_metadata', 'ea_default_term_description', 10, 4 );
// Use term description if empty
if( empty( $value ) ) {
$term = get_queried_object();
$value = $term->description;
}
}
return $value;
}
add_filter( 'get_term_metadata', 'ea_default_term_description', 10, 4 );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Alexandra Spalato says

    Hi,
    my client need to have the headline and the description, so i try to have the 2 snippets in my function, but i get a php error
    function be_default_category_title($headline, $term ) {
    if( ( is_category() || is_tag() || is_tax() ) && empty( $headline ) )
    $headline = $term->name;

    return $headline ;

    }
    add_filter( ‘genesis_term_meta_headline’, ‘be_default_category_title’, 10, 3 );

    function be_default_term_description( $desc, $term ) {
    if( ( is_category() || is_tag() || is_tax() ) && empty( $desc ) )
    $desc = $term->description;

    return $desc;
    }
    add_filter( ‘genesis_term_meta_intro_text’, ‘be_default_term_description’ );

    Also , i would like to know where is this filter genesis_term_meta_headline , serach in the genesis code and don’t find it

  2. Alexandra Spalato says

    I have actually solved the problem, and there is an error in the second snippet, as it miss the arguments

    so this is my complete code ant it works 🙂

    */
    function be_default_category_title($headline, $term ) {
    if( ( is_category() || is_tag() || is_tax() ) && empty( $headline ) )
    $headline = $term->name;

    return $headline ;

    }
    add_filter( ‘genesis_term_meta_headline’, ‘be_default_category_title’, 10, 2 );

    function be_default_term_description( $desc, $term ) {
    if( ( is_category() || is_tag() || is_tax() ) && empty( $desc ) )
    $desc = $term->description;

    return $desc;
    }
    add_filter( ‘genesis_term_meta_intro_text’, ‘be_default_term_description’, 10, 2 );

    As my client also wanted the description wrapped in h5 instead of p

    i add this filter

    as it take me sometimes to figure it out, i paste it here so it can be usefull for others 😉

    add_filter(‘genesis_term_intro_text_output’ , ‘wst_wrap_intro_text’);
    function wst_wrap_intro_text() {
    global $wp_query;
    $term = is_tax() ? get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) ) : $wp_query->get_queried_object();
    $intro_text = sprintf( ‘%s’, strip_tags( $term->meta[‘intro_text’] ) );
    return $intro_text;

    }

  3. Erik D. Slater says

    The problem with using the “Archive” Headline and Intro Text fields – compared to using the regular Name and Description fields – is that you can’t export the data … unless I missed something … which is quite possible.

    • Bill Erickson says

      That’s a good point. But if you use the approach outlined in the article above (using the category name for “Headline”) and duplicate it for the intro text (use description for “Intro text”), you solve that problem.

      You store the information in Name/Description which is core WP functionality (no need to migrate data), and you leverage Genesis’ built-in functionality for displaying an archive intro using that data.

      • Erik D. Slater says

        Yep, that’s exactly what I did … for both name and description 🙂

        Otherwise, you end up having to fiddle around with the genesis-term-meta entry in the options table … which I tried out for fun … and which works … but it’s just not terribly satisfying … even if it is good to know 🙂

        The nice thing about the snippet is that it still takes the Archive details if you fill them in … if that’s something someone wanted to do.

  4. Alexander Bock says

    Hey! Thank you for this nice snippet. What do you think about to remove the class=”archive-title”?
    Title
    I think Title is cleaner and maybe google like it more? Do you know how to remove class?
    Thank you again and nice website! 🙂

  5. Sue says

    The code snippet is not working on my site or other sites I have placed it on since the Genesis 2.2 update. Has anyone else had this happen?

    • Bill Erickson says

      Yes, there is a known issue in Genesis 2.2 where archive titles and descriptions do not show up unless you have ‘genesis-accessibility’ turned on. You can:

      a) Wait for Genesis 2.2.1 to fix this (should be in a day or two)
      b) Install the Genesis Accessible plugin, so your website will be accessible and archive titles/descriptions will show up
      c) Manually update your theme to be accessible. Here’s a tutorial.

  6. Adam Cap says

    Hey Bill, this stopped working for me in 2.2.7 unless accessibility for headings is enabled. In 2.2.5 /genesis/lib/structure/archive.php the heading check changed from …

    if ( $term->meta['headline'] ) {

    to …

    if ( $headline = get_term_meta( $term->term_id, 'headline', true ) ) {

    So the filter becomes fruitless. Not sure if there’s an elegant rewrite! You can always remove the genesis_do_taxonomy_title_description action and add your own custom one, but that’s about all I’m seeing.

    • Bill Erickson says

      Now that term meta is in WordPress core, we can switch from a Genesis filter to a WordPress filter: get_term_metadata. See wp-includes/meta.php

      I haven’t needed this function yet so I haven’t dived into recreating it, but if you are able to get it to work I’d love a follow-up comment.

      • Joshua Nelson says

        Bill, awesome! No sooner did you do that, than I realized there was an error in that code 😉 I’ve updated the gist accordingly and tested it again to be sure.

        Cheers,
        Joshua

        • Bill Erickson says

          I’ve cleaned up the code a bit. Instead of using query data to determine the term ($wp_query and get_query_var) I’m simply using the $term_id provided by the filter. You might want to update your code to do the same.

          While it most likely won’t matter in this specific instance because the headline is used on archive pages, I wanted to provide a good example of how to filter term metadata, and the filter itself shouldn’t depend on the page’s query.

  7. Jeremy says

    Hi Bill, I’m trying to create a modified version of this function which only works on one specific taxonomy, rather than working on all categories/tags/taxonomies. I want to modify it to prepend some text before the Archive Headline for that particular taxonomy.

    I’ve gotten the prepended text to work nicely, but I’m having problems getting my function to only work on the desired taxonomy. Here’s what I have:

    ——————
    function default_term_title( $value, $term_id, $meta_key, $single ) {
    if( is_tax ( ‘my_taxonomy_name’ ) && ‘headline’ == $meta_key && ! is_admin() ) {

    // Grab the current value, be sure to remove and re-add the hook to avoid infinite loops
    remove_action( ‘get_term_metadata’, ‘default_term_title’, 10 );
    $value = get_term_meta( $term_id, ‘headline’, true );
    add_action( ‘get_term_metadata’, ‘default_term_title’, 10, 4 );

    // Use term name if empty
    if( empty( $value ) ) {
    $term = get_term_by( ‘term_taxonomy_id’, $term_id );
    $value = ‘Prepended text here ‘ . $term->name;
    }
    }
    return $value;
    }
    add_filter( ‘get_term_metadata’, ‘default_term_title’, 10, 4 );
    ——————

    Now, if I set the 2nd line to be the following, with no parameters set for “is_tax”, then the function works fine – albeit for all my taxonomies, not just my desired one:

    if( is_tax ( ) && ‘headline’ == $meta_key && ! is_admin() ) {

    So the problem seems to be with me adding the name of my taxonomy inside the “is_tax” brackets. I’m confident that I’ve got the name correct. But when I save this function and then go to the Archive page of that taxonomy, I get a completely blank page. (Viewing the page source is also completely empty).

    Can you see what I’m doing wrong here? How can I specify this plugin to only work on the one desired taxonomy?

    • Bill Erickson says

      Hmm, the code should work as you have it. Have you tried turning on debug mode in wp-config.php?

      • Jeremy says

        I really can’t figure this out. I tried enabling debug mode by adding the following three lines to wp-config.php:

        define( ‘WP_DEBUG’, true );
        define( ‘WP_DEBUG_LOG’, true );
        define( ‘WP_DEBUG_DISPLAY’, true );

        But it didn’t seem to do anything. When I reloaded my taxonomy archive page, I just got a white screen of death still (as before). Shouldn’t I see debug messages on the page there? I also didn’t get any debug.log file created in my wp-content folder either.

        Tell me, if I change the 2nd line of the function to this (and change nothing else)….

        if( is_tax ( ‘my_taxonomy_name’ ) && ‘headline’ == $meta_key && ! is_admin() ) {

        …then do I also need to change anything in the first line too, for example adding an extra $ variable inside the brackets of “ea_default_term_title”?

        I wonder if you might be willing to test my function and see if it works for you, ie. limit your function to only trigger on one single taxonomy as I have.

        Note that the taxonomy I’m trying to use is from a custom post type that I have via a plugin. This plugin’s CPT includes several taxonomies, and I’m only wanting my function to trigger on one of them.

    • Joshua says

      Nevermind, I misread your comment. If it’s the only custom taxonomy, is_tax will return false on category and post_tag archives, so you would be covered there. Otherwise that should work as you show it.