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. Dave says

    Bill,
    I’ll second Craig’s comment, a very nice bit of code, so many of which you offer here.

    This comes up as a forum question sometimes, and now there’s a place to send them. 🙂

    Thanks,
    Dave

  2. Miranda says

    I’m always so impressed by the elegance of your solutions! I’ve added titles to archives several times in the past by putting code to retrieve and output the term into the appropriate template file, completely overlooking the built-in headline functionality. I’ll definitely be adding this into my base child theme for future use. Thanks Bill Erickson!

  3. Victor Marsala says

    I have a general question in terms of your approach to client edits.

    I have more and more clients that want to handle creating their own content and maintaining it, and while the days of pasted-in Microsoft Word headaches are nearly over, it seems counterintuitive to think just how few people know anything about HTML or how it works, or listen when provided with a few pieces of information relevant to get the job done.

    If someone wants a video gallery, rather than make them copy and paste divs and headers and URLs, I’ve sometimes just done it WerkPress style and made a custom post type for videos and told them each video “post” is aggregated on the gallery page. And that’s fine.

    But recently I keep having things like someone who wants a simple schedule, where you click the times and it reveals in a jQuery slideToggle what takes place at that time. I say just copy and paste the two lines, and edit the text of it for the bold and non-bold bits, and the script will do the rest.

    It seems like overkill to make an entire post for two lines of text, so I don’t want to do that to them, but if they edit the text in the Visual tab, WordPress can mess with the formatting, and if they copy and paste in the Text tab, I’ve seen three different ways the formatting can be broken at the same time on the same page. Even something as simple as getting the nesting wrong can be disastrous.

    The themes run great (thanks, Genesis) and the actual custom coding and scripting are effective at what they do. But without some protected way of editing specific areas (almost like Publisher, or vaguely like Photoshop layers) I don’t know how to save them from themselves. Do you just make redundant code that tries to sniff out every possible break? It seems awkward and limited.

    • Bill Erickson says

      I make extensive use of custom metaboxes. My goal is for clients to never see (or need to know) HTML. So if there’s anything that can’t be done easily through the visual editor, I’ll build a metabox for the client to manage it.

      For instance, here’s a page on a client site, and here’s what the backend looks like to manage it: http://cl.ly/image/2s3o22373Q3h.

      • Victor Marsala says

        Wow, that’s an interesting take! A lot going on, but highly organized. My first thought was “How does it fit for a schedule scenario,” but the whole idea of a schedule is that everything’s fixed, and the only things they should be editing anyway are time ranges and descriptions, not how many entries there are.

        Thanks for the feedback. You’re always so professional and generous with what you share and I hope nothing but continued success for you. Thanks for your positive contributions to both WP and Genesis and their respective communities!

  4. Joe Banks says

    Bill,

    Thanks for writing this up.

    Here’s the possibly-slap-my-forehead question for you.

    Why does Genesis include the extra “Archive Headline” and “Archive Intro Text” on category & tag pages when WordPress already supplies the “Name” and “Description” fields? For our Genesis theme, long ago I added code to the functions file to simply write the WordPress “Name” and “Description” on category & tag pages, due to this confusion you speak of.

    Thank you.

    • Bill Erickson says

      I’m not sure why Genesis creates its own fields. My guess is so that you could have a title that’s different than the category name. So the category might be “News” and your archive title is “News about Company XYZ”. Alternatively, you might add descriptions to your categories to be used in the backend or in mega menus, but not want them displayed at the top of the archive pages.

      I think the simplest answer is that by providing additional fields, Genesis doesn’t have to assume how you want things structured. I prefer the “Decisions, not Options” approach personally.

  5. Mario says

    Thank you very much for sharing this. How should I modify the code to be able to show also the archive intro text for each category automatically?

  6. Mario says

    Thanks Bill, you’re amazing. Sorry if I ask it here: have you already made one of your beatifully neat snippets for managing sidebars and menus? I’ve been using the plugins “Simple Sidebars” and “Simple Menus”, but they seems to be able to modify only the pages created by the user, while I need to dynamically change also the sidebars and menus displayed in 404, archives and not found pages.

      • Mario says

        I see, so the only way is to do it manually… I’ve added your code and it works like a charm, but my sidebar is now empty obviously. Could you please tell me also how to add widgets to it? I hope I’m not bothering you too much with my questions.

        • Bill Erickson says

          That code creates a sidebar called “Archive Sidebar”, so go to Appearance > Widgets and add widgets to the Archive Sidebar.

          • Mario says

            I just realized why I couldn’t see the sidebar in Appearance; I had to add a piace of code to the register_sidebar including the name of my theme. Which is the Is_function() for applying it also to search pages with results and not found search pages? Again, thank you for everything, I’m very satisfied.

  7. Mario says

    I found out how to do it. You can remove this and the previous message, thanks. 🙂

  8. Mario says

    How can I make a search form like yours in Genesis 2.x ? I mean with the button inside the search bar with a lens image.

      • Mario says

        I’ve already tryed it, and before it, even the Yoast one made by you. It seems that there is something wrong because Genesis Framework forces the text “Search” in the submit, so I cannot achieve the result that I want because the submit size cannot be less than the font size of it (or that’s what I assumed).

        Have a look at the problem, please: http://imgur.com/IyH6Y9O

        • Bill Erickson says

          Add this to your functions.php file:

          add_filter( ‘genesis_search_button_text’, ‘__return_false’ );

          Genesis adds text to the search field and the submit field, but these are both filterable.

          • Mario says

            Just added your snippet but the result is quite the same. I even used your exact same image and style, so I can’t see where is the problem. Anyway, it seems to work fine now for sizes bigger than 48x32px, really strange thing… Is it maybe declared as minimum size somewhere?