Archived topic
Appendtext after archive title
15 replies · Started by George on June 6, 2018
I thought that would be easy but I can't get it to work. I am trying to append some text after a custom post type category archive's title:
add_filter( 'generate_after_archive_title', 'text_top' );
function text_top( $text ) {
if ( is_category('Useful Links') )
return 'Some text here';
return $text;
}
I don't get anything.
You may need to use this function instead of is_category(): https://codex.wordpress.org/Function_Reference/is_post_type_archive
Yeah, I tried that before also Tom, didn't work either. I am really confused with the is_post_type_archive() function, sometimes it works, sometimes it doesn't. That's why I tried using is_category(). The page is basically listing a category taxonomy so is_category() should work.
I have also tried removing the conditional
add_filter( 'generate_after_archive_title', 'text_top' );
function text_top( $text ) {
$text == "Some text here";
return $text;
}
and it didn't work either so I think something fishy is going on!
Ah, generate_after_archive_title isn't the filter name.
Try this filter name instead: get_the_archive_title
This works but it's replacing the archive heading. I want to append a paragraph after the archive heading (I am hiding the heading anyway!).
Try this:
add_filter( 'get_the_archive_title', 'text_top' );
function text_top( $text ) {
return $text . ' Your appended text';
}
I knew there was something fishy going on! The code doesn't work when inserted in the code snippets plugin but works fine when inserted in the functions.php file! Have you ever had that issue before, Tom?
Ah yea, action order. Plugin function fire before theme functions, and GP uses that filter, which was probably overwriting your function.
Could I make it work with the code snippets plugin activated? I tried the solution found here but it didn't work.
This doesn't work?
add_action( 'after_setup_theme', 'tu_add_archive_title_filter' );
function tu_add_archive_title_filter() {
add_filter( 'get_the_archive_title', 'text_top' );
}
function text_top( $text ) {
return $text . ' Your appended text';
}
Great, that works Tom, I was shaping my function differently. Now back to the original problem: The current function just appends to the existing title. I would like to add a paragraph after the archive title. I tried:
add_action( 'after_setup_theme', 'tu_add_archive_title_filter' );
function tu_add_archive_title_filter() {
add_filter( 'generate_after_archive_title', 'text_top' );
}
function text_top( $text ) {
return ' Your appended text';
}
but it didn't work. You mentioned that this was not the right function though. What is the function to insert a paragraph after the archive title?
My function above should do exactly that: https://generatepress.com/forums/topic/appendtext-after-archive-title/#post-595562
Notice the $text variable within the return string. That $text variable holds the existing title.
Yes, I know. But this example just appends something on the existing title. So, for instance instead of having a title "Existing Links", the archive title becomes "Useful Links Your appended text". It still manipulates the current archive title. I would like to add a paragraph AFTER the title. I don't understand why generate_after_archive_title is not working, it seems that this is the right hook from the documentation.
Ah, I was confused - I'm sorry!
You almost had it right in the first place, but you were using add_filter() instead of add_action():
add_action( 'generate_after_archive_title', 'text_top' );
Ah! I didn't even need the variable inside the function then! That worked:
add_action( 'generate_after_archive_title', 'text_top' );
function text_top() { ?>
<p>Some text here</p>
<?php }
Great, thanks Tom!