Community Forums › Forums › Archived Forums › General Discussion › Add "title" in "archive" pages, like category or tag's lists
- This topic has 4 replies, 2 voices, and was last updated 7 years, 8 months ago by DanielMT.
-
AuthorPosts
-
June 28, 2016 at 2:08 am #188424DanielMTParticipant
Hi,
I'm wondering if there's a way to display a page "title" when a "category" or "tag" archives are displayed. For example, take a look at this screenshot: https://www.evernote.com/l/AGx02-IiV59I_rv_53YFX-NADcHiyn3y7ow
This is what the user will see when clicking a post's tag. The same for a category. Is there a way to show such a title? For example: "Recipes from the category XXXXXXXX"
Just a snippet to add in the functions.php file.
Thanks in advance,
DanielJune 28, 2016 at 9:08 am #188446ChristophMemberHi Daniel,
that´s built-in functionality.
Go to Posts - Categories - Edit the category - Scroll down to Category Archive Settings
And enter an Archive Headline.
http://prntscr.com/bm5zsi
Click UpdateLooks like this:
http://prntscr.com/bm601jIt's the same for Tags
June 28, 2016 at 9:13 am #188448DanielMTParticipantHi Christoph,
Thanks for the answer.
I know about that functionality, but I have more than a hundred tags and I don't want to edit one by one just to add that title. I'm looking for doing it with a simple snippet.Thanks,
Daniel.June 28, 2016 at 9:55 am #188453ChristophMemberHi Daniel,
that makes a lot of sense 🙂
Bill Erickson to the rescue:
(I just added the 'Recipies from the ' before the term name 😉 )
http://prntscr.com/bm6nns/** * Default Titles for Term Archives * * @author Bill Erickson * @see http://www.billerickson.net/default-category-and-tag-titles * * @param string $headline * @param object $term * @return string $headline */ 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_term_by( 'term_taxonomy_id', $term_id ); $value = 'Recipies from the ' . $term->name; } } return $value; } add_filter( 'get_term_metadata', 'ea_default_term_title', 10, 4 );
June 29, 2016 at 2:29 am #188507DanielMTParticipantThanks for your suggestion, but didn't work fine.
Finally, I've built this snippet and put it in a hook using Genesis Simple Hooks:
<?php if (is_category()) { echo '<h1 class="entry-title" itemprop="headline">Recipes from the category "' . single_term_title("", false) . '"</h1>'; } if (is_tag()) { echo '<h1 class="entry-title" itemprop="headline">Recipes tagged with "' . single_term_title("", false) . '"</h1>'; } ?>
Regards.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.