[Resolved] Appendtext after archive title

Home Forums Support [Resolved] Appendtext after archive title

Home Forums Support Appendtext after archive title

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #593565
    George

    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.

    #593652
    Tom
    Lead Developer
    Lead Developer

    You may need to use this function instead of is_category(): https://codex.wordpress.org/Function_Reference/is_post_type_archive

    #593808
    George

    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!

    #594172
    Tom
    Lead Developer
    Lead Developer

    Ah, generate_after_archive_title isn’t the filter name.

    Try this filter name instead: get_the_archive_title

    #594241
    George

    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!).

    #594785
    Tom
    Lead Developer
    Lead Developer

    Try this:

    add_filter( 'get_the_archive_title', 'text_top' );
    function text_top( $text ) {
        return $text . ' Your appended text';
    }
    #595294
    George

    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?

    #595431
    Tom
    Lead Developer
    Lead Developer

    Ah yea, action order. Plugin function fire before theme functions, and GP uses that filter, which was probably overwriting your function.

    #595440
    George

    Could I make it work with the code snippets plugin activated? I tried the solution found here but it didn’t work.

    #595562
    Tom
    Lead Developer
    Lead Developer

    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';
    }
    #595603
    George

    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?

    #595611
    Tom
    Lead Developer
    Lead Developer

    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.

    #595618
    George

    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.

    #595687
    Tom
    Lead Developer
    Lead Developer

    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' );

    #595837
    George

    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!

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.