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

basic genesis wordpress

Receive New Posts by Email

Comments

  1. 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. 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. 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.

    1. 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.

      1. 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. 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.

    1. 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. 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. 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.

      1. 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.

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

          1. 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. 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.

      1. 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

        1. 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.

          1. 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?

  8. 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.

  9. 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.

  10. 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!

    1. 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/

      1. 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.

        1. 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!

  11. 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?

    1. 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.

      1. 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?

        1. 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.

  12. 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.

    1. 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.

  13. 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.

    1. 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).

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

        1. 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.

  14. 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.

    1. 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.

      1. 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.

  15. 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.

    1. 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.

      1. 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?

        1. 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.

  16. 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.

    1. 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.

      1. 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? 🙂

        1. 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.

          1. 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?

          2. …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.

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

      2. 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!

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

  17. 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 !

  18. 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

  19. 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;

    }

  20. 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.

    1. 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.

      1. 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.

  21. 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! 🙂

  22. 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?

    1. 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.

  23. 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.

    1. 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.

      1. 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

        1. 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.

  24. 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?

      1. 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.

    1. 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.

  25. Hi Bill,

    I’m having a really strange issue implementing this one, and I can’t for the life of me work out what the issue is.

    For some reason, the filter is outputting the name of the category that has an ID 1 less than the current category… i.e. if I’m on category ID 161, it’s outputting the name of category ID 160.

    I can fix this by changing line 24 to:
    $term = get_term_by( ‘term_taxonomy_id’, $term_id+1 );

    However I’m a bit worried that something will change in a future update and I won’t notice that all my categories are displaying the wrong name! Any idea what could be going on here??

    Thanks for taking the time to write this and your many other snippets by the way – they’re extremely useful and have helped me no end!

      1. Ok, I’ve done a bit more digging and I think the issue comes from the fact that to get the term, we’re using the term_taxonomy_id and term_id interchangably:
        get_term_by( ‘term_taxonomy_id’, $term_id );

        However, while the term_taxonomy_id and the term_id are often the same, they’re not in every case. I’ve found that for some reason, on my site they are offset by one.

        This is most likely the cause of the issue Jeremy (above) is having also? Are you able to check if my suspicions are in the right direction…?

        1. Ben, if you have a copy of the original function which you’ve adjusted/fixed based on your theory, I’d be happy to try it out and see if it fixes my issue! 🙂

          1. Jeremy, I’ll let you know if I get it working!

            I would have thought only using the $term_id variable in the get_term_by function would have solved the issue:

            if( empty( $value ) ) {
            $term = get_term_by( 'id', $term_id );
            $value = $term->name;
            }

            However, that part of the function doesn’t set the $value variable to anything, it remains empty. I’ll keep you posted…

          2. If you’re using the $term_id you also have to pass the $taxonomy.

            $term = get_term_by( 'id', $term_id, $taxonomy );

            Up until WordPress 4.2, term IDs were only unique for the taxonomy, not site-wide. So you might have two terms with the same ID but in different taxonomies. See taxonomy term splitting in WordPress 4.2 for more details.

  26. Hi Bill

    I want to prefix my category with something like ‘Specialists in THE CATEGORY’ as the title of the Category archive. can you suggest how I do this?

    Thanks

  27. Hi Bill, your function was working completely fine when I tested earlier. But after that, I had to remove around 500 unused tags from the website. After deleting these tags, the tag shows title of some other tags. Is it something related to tag id? I am not able to figure out how to rectify this. Please help.

    1. I can’t imagine how that would happen. The titles are stored as metadata based on the $term_id and that should never change. Deleting tags should not affect the term_id’s on existing, non-deleted tags.

    2. Had the same problem!
      Fixed it by using $term = get_term($term_id);
      instead of $term = get_term_by( ‘term_taxonomy_id’, $term_id );

  28. Hi Bill, this function is working great for me. I’d really love to extend it so that it will default the “Archive Intro Text” field to whatever is written in the “Description” field above it, if “Archive Intro Text” is empty.

    That way, I can leave both “Archive Headline” and “Archive Intro Text” empty, and just have text in the “Name” and “Description” fields, and that text will be used instead – this will save even more time.

    Is it possible to extend your function here to achieve this? I guess it would be more efficient to extend the existing function, rather than creating a new and separate function?

      1. Thanks so much Bill, that works perfectly!

        Is it possible to combine the two into one function? Would that be more efficient, as it would only be making one query? Or not really?

  29. I read all the comments, and either I missed it, or misunderstood, but I’ve looked everywhere for a way to show the Archive Intro Text only on the first page of the category archive. Any idea how to do that?

    Thanks a lot for the help.

  30. Hi Bill,

    Looks like you forgot to update the @param and @return in the Docblock sections in the code snippets.

  31. Hi Bill,
    as of now Oct 2017, with the code above I get the following error. May be an update to the code snippet to be compatible with Genesis v2.5.x ?


    Trying to get property of non-object

    It affects the line:

    $headline = $term->name;

    1. I’ve updated the code in the post. I actually ran into this issue a few weeks ago and forgot to update it here, so thanks for reminding me!

      Since this code only runs on the category/tag/term archive page, there’s no reason to run another query for the term. We can just use get_queried_object().

  32. When I try this it actually crashes my website.

    I was hoping to make the default title say “Here’s everything filed under [Category]”, but changeable if I input something else in the Archive Headline field. Is there an easy way of adding this?

Leave a comment