Archived topic
Headings h1
9 replies · Started by Roman on February 2, 2018
Hi there
On category and post pages in heading h1 shows category name or post title.
I have two questions:
1. For categorys. How to remove this heading entirely (with h1 tag)?
2. For both. How to replace h1 tag contents by my custom field from ACF?
Hi there,
Not too sure what you mean by post pages? Single posts?
For the categories, you can do this:
add_filter( 'get_the_archive_title', 'tu_custom_archive_title' );
function tu_custom_archive_title( $title ) {
if ( is_category() ) {
// Your custom ACF field here
}
return $title;
}
Yep, single posts.
This code doesn't work, but thanks for directing me to the right way :)
Here is the code for categories (replaces title with advanced custom field):
add_filter( 'single_cat_title', 'tu_custom_archive_title' );
function tu_custom_archive_title( $title ) {
$term = get_queried_object();
$mycattitle = get_field('acf_category_title', $term);
if ( $mycattitle != '' ) {
return $mycattitle;
}
else {
return $title;
}
}
Can you help me do the same for single posts?
For single posts, I believe you would use the_title filter.
Let me know if that does it or not :)
Emmm. It works, but it also replaces text of almost all links with my custom field :) Navigation, sidebar, prev and next post links - all have my title instead link text.
I tried to move the code from snippet to hook (before content). Now navigation is fine, but all that after this hook has changed.
Here is the code:
add_filter( 'the_title', 'tu_custom_post_title' );
function tu_custom_post_title( $title ) {
$term = get_queried_object();
$myposttitle = get_field('acf_post_title', $term);
if ( $myposttitle != '' ) {
return $myposttitle;
}
else {
return $title;
}
}
Hi there,
Try this instead:
add_filter( 'the_title', 'tu_custom_post_title' );
function tu_custom_post_title( $title ) {
$term = get_queried_object();
$myposttitle = get_field('acf_post_title', $term);
if ( $myposttitle != '' && in_the_loop() ) {
return $myposttitle;
}
else {
return $title;
}
}
Better, near perfect ) But prev and next links still have my title. Because they have %title too, I think.
I wonder if adding this would fix it:
add_action( 'generate_after_entry_header', 'tu_remove_title_filter' );
function tu_remove_title_filter() {
remove_filter( 'the_title', 'tu_custom_post_title' );
}
Bit of a long shot.. Let me know :)
Now everything is perfect ) Thanks!
You're welcome :)