- This topic has 15 replies, 2 voices, and was last updated 5 years, 10 months ago by Tom.
-
AuthorPosts
-
June 6, 2018 at 6:58 pm #593565George
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.
June 6, 2018 at 9:24 pm #593652TomLead DeveloperLead DeveloperYou may need to use this function instead of
is_category()
: https://codex.wordpress.org/Function_Reference/is_post_type_archiveJune 7, 2018 at 2:21 am #593808GeorgeYeah, 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 usingis_category()
. The page is basically listing a category taxonomy sois_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!
June 7, 2018 at 9:00 am #594172TomLead DeveloperLead DeveloperAh,
generate_after_archive_title
isn’t the filter name.Try this filter name instead:
get_the_archive_title
June 7, 2018 at 10:28 am #594241GeorgeThis works but it’s replacing the archive heading. I want to append a paragraph after the archive heading (I am hiding the heading anyway!).
June 7, 2018 at 9:21 pm #594785TomLead DeveloperLead DeveloperTry this:
add_filter( 'get_the_archive_title', 'text_top' ); function text_top( $text ) { return $text . ' Your appended text'; }
June 8, 2018 at 7:55 am #595294GeorgeI 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?
June 8, 2018 at 11:17 am #595431TomLead DeveloperLead DeveloperAh yea, action order. Plugin function fire before theme functions, and GP uses that filter, which was probably overwriting your function.
June 8, 2018 at 11:26 am #595440GeorgeCould I make it work with the code snippets plugin activated? I tried the solution found here but it didn’t work.
June 8, 2018 at 4:02 pm #595562TomLead DeveloperLead DeveloperThis 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'; }
June 8, 2018 at 4:55 pm #595603GeorgeGreat, 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?
June 8, 2018 at 5:16 pm #595611TomLead DeveloperLead DeveloperMy 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.June 8, 2018 at 5:23 pm #595618GeorgeYes, 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.June 8, 2018 at 8:53 pm #595687TomLead DeveloperLead DeveloperAh, I was confused – I’m sorry!
You almost had it right in the first place, but you were using
add_filter()
instead ofadd_action()
:add_action( 'generate_after_archive_title', 'text_top' );
June 9, 2018 at 4:12 am #595837GeorgeAh! 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!
-
AuthorPosts
- You must be logged in to reply to this topic.