Archived topic
Filter category title
9 replies · Started by Tesco on August 5, 2022
Just saw the filters list and I didn't see any one that could fit...
Is there any way of filtering the category and archives title?
Hi there,
GP uses the core get_the_archive_title function, which has its own hook:
https://developer.wordpress.org/reference/hooks/get_the_archive_title/
Hi David,
Can you help me out here?
I'm trying this code but it's not working...
add_filter( 'get_the_archive_title', 'f_archive_title');
function f_archive_title ($title) {
// variable
$my_variable = "This is my variable to replace the cat title";
if ( is_category() ) {
$title= '<h1 class="entry-title"%1$s>%2$s ' . $cat_meta['h1_custom_title'] . 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '</h1>';
}
return $title;
};
What is it meant to do ?
Replace the h1 title by a string in a variable
I don't know what this is for:
$time = get_post_meta($post->ID, 'time', true);
And haw was the option added to the Category Term? And does that work ?
Last thing i can say is $title is a string, you don't need the HTML as you're only returning the string value.
You can forget $time = get_post_meta($post->ID, 'time', true); and the option added to the Category Term.
I just need a way to change the category title by a value returned from my database what will be the string inside the variable (it can be any variable) I mentioned earlier.
Corrected the code above...
I found out that this is the way the theme renders what I need to filter using the function generate_archive_title:
<h1 class="page-title"><?php the_archive_title(); ?></h1>
Desired in archive.php is:
Replace this: <h1>Category Name</h1>
By this: <h1>My Custom Category Title</h1>
This is the basics of the function:
add_filter( 'get_the_archive_title', 'f_archive_title', 999 );
function f_archive_title ($title) {
$custom_title = 'your custom title';
if ( is_category() ) {
$title = $custom_title ;
}
return $title;
};
Notes:
the 999 on the first line, that is the filter hook Priority and it will make sure that THIS function callback is after all the others.
the $custom_title is just the text string, theres no need for the HTML as the Theme does that for you.
Now you just need to decide on where you will save your custom title and load that into the $custom_title variable
You're great (as usual)! I appreciated the explanations.
Thanks!
Glad to be of help!