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

    Ok, I’ve found the problem… There was a padding overwriting another padding around in the code. It works even keeping the value attribute now, thank you.

  2. Mario says

    Ok, now I have another problem… I had to change the padding in input [type=”submit”] to 0 to fix the problem of the area around the button, but now the “Post Comment” button, being also it an input submit, has no more padding… Which is the best way to solve this in your opinion? The only idea that came into my mind is using width and height instead of padding.

  3. Mickey Kay says

    Hey Bill,

    Thanks for the great snippet. I would love to see an all inclusive solution that covers Author and Date archives as well (along with any other conceivable archive). Any ideas?

    Seems like the genesis_term_meta_headline filter only works for taxonomy terms, and I can’t seem to find a good filter to hook into for the author title, which simply relies on:
    $headline = get_the_author_meta( ‘headline’, (int) get_query_var( ‘author’ ) );

    For some reason, the author intro text is passed through a filter (I wonder why the author headline isn’t):
    $intro_text = $intro_text ? apply_filters( ‘genesis_author_intro_text_output’, $intro_text ) : ”;

    Problem with this code is that if $intro_text is blank, you’re still going to get ”, regardless of what you try to do with that filter. It should definitely be filtering before checking for a non-empty value, no? Am I missing something?

    Same goes for date archives – I can’t seem to find any code for outputting a title for these archives. Is that so?

    It seems like it might be helpful if ALL primary / page title output was wrapped in a common filter, including titles on the various archive pages. This would allow for much simpler modifications across the board (e.g. adding an icon before each page title or writing one function with a bunch of conditionals that can easily set default titles for ALL different types of archives).

    Is this doable in another way? Would love your feedback.

    Thanks again for the awesome snippet!

    • Bill Erickson says

      The author archive typically has the author box displayed at the top, if that user has checked “display author box” on their profile. You can automatically display this (as if all authors had already checked that) using this code: https://www.billerickson.net/code/automatically-display-author-box/ One is for displaying the author box on author archives, the other is to display at the bottom of posts.

      You’d need to write your own for date archives. This will work: https://www.billerickson.net/code/header-date-archives/

      • MIckey Kay says

        Fantastic, thanks Bill! Any issues you can foresee were I to create an “Automatic Archive Titles” plugin that combines all three methods? Are there other archives you know of that would need including (right now it’s term, date, author)? Other thoughts?

        Thanks again for the great snippets.

        • Bill Erickson says

          Well the term and author ones I shared would only work in Genesis. And you might want to create a settings page where people could turn them on/off (ex: none of the authors have filled out bios, so lets not turn on author archive intros). But other than that, you’re good!

  4. Mario says

    Yeah, I solved it doing so. 🙂 I couldn’t understand the situation because of input, input[type=”submit”] and .search-form input[type=”submit”].

    I can’t find any snippets for removing this annoying label: Comment
    So many guides on the Net for removing or customizing name ,email and url but not this one. How did you manage to remove it in your comment form?

    • Bill Erickson says

      In the HTML4 version of Genesis (which is what I’m still using), Genesis had its own comment form arguments and didn’t include a label for comments. The HTML5 version uses WordPress’ default comment form settings which does.

      Look at /genesis/lib/structure/comments.php, genesis_comment_form_args() to see how it’s being filtered for HTML4. All that’s relevant to you is the ‘comment_field’ property.

      • Mario says

        I found it… So, the only way is to create an own HTML5 comment_form using php instead of the one used by default for WordPress, right? I will keep it then… When I tryed to move the elements using CSS, I couldn’t get a proper result, even though the code alone was right and working, I don’t know why.
        Anyway, the bad thing of the WordPress comment_form is that it takes too much space to display vertically, which I don’t like honestly. Is it possible they will change it in the future?

        • Bill Erickson says

          No, you don’t have to rebuild the whole form. The filter I showed you lets you modify just the ‘comment_field’, which is the textarea for the comment and its label.

          You could also just hide it with CSS.

  5. Mario says

    Is there a remove_action for comment_author_says_text? Using __return_false there is still the span class and I don’t want to touch Genesis core files.

    • Bill Erickson says

      You’ll need to completely rebuild the comment output. Filter ‘be_comment_list_args’, change the callback to a function you create. Copy the callback in /genesis/lib/functions/comments.php as a good starting place.

      Although you bring up a good point. I’ll post that as an internal Genesis issue and hopefully the next version will check to see if there’s a value for “says” before outputting the markup.

  6. Mario says

    Is there a way to manually rearrange the cropped part shown as featured/grid image? I mean, sometimes you put an image and it automatically shows the middle part of it while you wanted the top one and so on. I would need to have 3 different sizes: featured image, featured grid and featured sidebar, of which I care just of width size that must be the one defined by me, and the other size to show in the height I would need to be able to manually fix it If I don’t like the default one shown.

    • Bill Erickson says

      Use my Image Override plugin. It allows you to upload an image for a specific image size rather than using the one WP generates. Just make sure you crop it to the right dimensions, because WordPress won’t scale and crop it for you (by design).

      • Mario says

        This is amazing! Just one questione: why it says “this image should be EXACTLY” for every size except large and medium?

        • Bill Erickson says

          Image sizes can either be hard crop, where the image is scaled down and then cropped to those exact dimensions, or they are soft cropped, where the image is scaled down to fit within those dimensions.

          The “exactly” means that image size is a hard crop.

  7. Mario says

    Do you recommend me to register new image sizes or replace the already existing “thumbnail”, “medium” and “large”? I think it would be better to unregister the WordPress ones and use mine, but I don’t know if it’s possible actually.

    Another strange thing I’ve noticed is that the featured image is shown in front-page/archive inside my content, just after the post-info (which is exactly what I want), while in the singular post it is displayed between the title and post info, and in original size, not the one defined by me. So, how can I tell Genesis to move it in my content (the default position, under the post info) also in my singular posts and to use my-cropped-size for it? The problem could be summarised in: “I want my featured images to look the same everywhere”. Thank you in advance man.

    • Bill Erickson says

      You can create a new image size if you need it, but I recommend using the three built-in ones first. WordPress will generate an image for every image size you have. So if you have the three built-in image sizes, you’ll have 4x the images you upload (original + 3 image sizes). Each additional image size will drastically increase the space used on your server, since every single image uploaded will also be generated at that size too.

      Genesis does not display an image on single posts/pages. It only displays it on archive pages. You can select which is used in Genesis > Theme Settings. My guess is your theme is adding an image on single posts, so look in single.php or functions.php to adjust it.

      • Mario says

        Actually you’re right, indeed I had a snippet in my functions.php. But what if I want my featured image to show also in my single posts? Would it be better to use an attachment every single time or put a snippet in functions.php telling Genesis to generate it in the right size, like for the archive? Maybe for a SEO purpose an attached image is more considerated, I don’t know.

  8. Mario says

    UPDATE: I tryed your “Image Override” plugin, but doesn’t seem to work. I upload the image, but it is stored as separated item and doesn’t actually replace the size chosen by me. Maybe it is due to new versions of WordPress.

    • Bill Erickson says

      I’ve tried it in the latest version of WordPress and there is no issues. The only possible source of your issue (that I can think of) is your theme isn’t actually calling the image size you are specifying. If you are overriding ‘medium’, your theme needs to be doing get_post_thumbnail( get_the_ID(), ‘medium’ ).

      You’ll need to troubleshoot the issue on your own as I don’t provide support for my plugins. But if uploading images directly to your uploads folder works for you, go for it. Just remember not to regenerate thumbnails.

      • Mario says

        Because your plugin is able also to keep the images uploaded by you manually even when you regenerate thumbnails?

        Anyway, could you please reply to the previous message on how to tell Genesis to use my size for the featured image and how to move it in the content, please?

        • Bill Erickson says

          I’m not sure what you’re asking about, regarding “attached images”. If you want the image to appear on single posts you’ll need to add code to functions.php or single.php to display it.

          I’m sorry, but these questions are no longer relevant to this post. I don’t typically provide this much free support. If you have more general Genesis questions, I recommend you post them in the Genesis support forums.

  9. Jeremy says

    Hi Bill, thanks for this helpful post! Can you tell me if there is any way to make the Archive Headline (and optionally Intro Text) appear at the top of every archive subpage too? (I mean /page/2/ and so on). Currently the subpages don’t have any heading at all – they just list the posts for that page, which can be confusing from a navigational point-of-view.

    At a minimum I’d love to just have the Archive Headline (via H1 tag) and the Intro Text displayed at the top of every subpage (identical to how it’s displayed at the top of the first page). But even better would be to make the heading specific to each subpage, like: “Category Name – page 3 of 8” – which is exactly how my HTML title tag looks anyway, due to using the WP SEO plugin which customises the title tag this way.

    Is there any function which can accomplish this? I haven’t been able to find a solution yet.

    • Bill Erickson says

      The archive intro Genesis provides is hardcoded to only work on the first page. If you look in /genesis/lib/structure/archive.php, line 39-40 is this:

      if ( get_query_var( ‘paged’ ) >= 2 )
      return;

      You’ll want to unhook the Genesis archive:

      remove_action( ‘genesis_before_loop’, ‘genesis_do_taxonomy_title_description’, 15 );

      Then build your own that functions as you like. You could copy that one as a guide, or build from scratch.

      • Jeremy says

        Thanks Bill, that was very helpful! I’ve now been able to make a new function based on the existing one, which adds the heading to every other archive page. Works great!

        Do you know why Genesis restricts the heading to just the first page, in the first place? Is there any particular reason for that, eg. SEO or something? I’ve read that some of the stock WP themes, eg. Twenty Eleven etc, show the heading on every archive page. So it puzzles me why Genesis specifically blocks that. Do you have any opinion about it yourself – advise for or against? 🙂

        • Bill Erickson says

          I think it is so that you don’t have the same h1 on multiple archive pages. But if you’re including a page number in there it shouldn’t be a problem.

          • Jeremy says

            Oh – and is it a problem if I *do* have the same h1 on multiple pages?

            What I’ve done in the end, is indeed to keep the h1 heading identical on every archive page – it was simpler that way. But the page *content* is obviously different, and each page also shows the pagination numbers to indicate which page you’re currently on. Surely that’s not a problem?

            I read a post from Google where they talk about “Demystifying the duplicate content penalty”(http://googlewebmastercentral.blogspot.de/2008/09/demystifying-duplicate-content-penalty.html) which seems to suggest that duplicate headings isn’t a problem? What do you think?

          • Jeremy says

            …I should also add that I have set all of my archive subpages to noindex, via Joust’s WordPress SEO plugin. So only the first page should be indexed anyway.

          • Bill Erickson says

            I’m not an SEO expert so I cannot provide guidance. I think you’ll be fine since they are noindex anyway.

      • Jeremy says

        Hi Bill,
        Back in 2014 you helped me with adding the Archive headline to every page in the archive, not only the first page. You explained via this comment:
        https://www.billerickson.net/default-category-and-tag-titles/#comment-101885
        …that “the archive intro Genesis provides is hardcoded to only work on the first page”, and your tip helped me to create a function that added the archive headline onto the other archives pages too, and it worked great.

        I noticed a little while ago that my function now causes the headline to be added a second time to the archive pages after page 1. So those pages now have the headline twice. If I remove my function, then those pages just show the headline once.

        My suspicion is that since 2014, Genesis has modified its built-in behaviour to show the archive headline on every single page, and not only on the first page as it did back then.

        Could you confirm if my suspicion is correct? If so, then I can safely delete that function of mine and not use it anymore.

        Thanks!

        • Bill Erickson says

          Yes, it appears Genesis is applying archive headings to all pages in the archive. See /genesis/lib/structure/archive.php for the relevant functions.

  10. Alex says

    Thank you,

    I use Eleven40 PRO theme and have had a problem naming TAG archive. With “Category” all was OK. With this add to theme function.php my problems are solved. Thank you !