Remove a word from end of post titles

Community Forums Forums Design Tips and Tricks Remove a word from end of post titles

This topic is: not resolved

Tagged: , , ,

This topic contains 6 replies, has 2 voices, and was last updated by  hansel 7 months, 4 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #89136

    hansel
    Member
    Post count: 4

    I would like to remove the last word of post titles (only on home and category pages).
    I tried this (and many more) but I can’t get it to work.

    This does not work:

    
    if ( is_home() || is_category() ) {
    	add_filter( 'genesis_post_title_output', 'child_remove_stupidword_from_titles' );
    	function child_remove_stupidword_from_titles( $title ) {
    		return preg_replace( 'stupidword$', '', $title );
    	}
    }
    

    How can I change the post title? I already tried many different hooks and no google results can help me out… Someone please help me out! :)

    #89145

    ramseyp
    Participant
    Post count: 99

    The hook that touches the title text is genesis_post_title_text, not genesis_post_title_output. Try running add_filter on that & see if it works.


    #89150

    hansel
    Member
    Post count: 4

    Unfortunately this doesn’t work either.

    Right now I have this in functions.php and it does nothing:

    
    if ( is_home() || is_category() ) {
    	add_filter( 'genesis_post_title_text', 'child_remove_stupidword_from_titles' );
    	function child_remove_stupidword_from_titles( $title ) {
    		return preg_replace( 'stupidword$', '', $title );
    	}
    }
    

    :( I hope someone can point me in the right direction…

    #89618

    hansel
    Member
    Post count: 4

    I sure hope someone comes along to help me…
    I had no luck over the weekend trying to fix this myself.

    #91498

    hansel
    Member
    Post count: 4

    Is there really no way to do this?

    #91500

    ramseyp
    Participant
    Post count: 99

    Hi Hansel,

    You want to rework your filter function a bit. You need to return $title, and the conditionals need to be moved inside the function, around what is to be changed when those conditionals are met. I used a different method of stripping the last word & it works well. I don’t know if using substr with strrpos way is faster than using preg_replace, but that would be an option, too, I think.

    function child_remove_stupidword_from_titles( $title ) {
    	if ( is_home() || is_category() ) { 
    		$title = substr($title, 0, strrpos($title, " "));
    	}
    	return $title;
    }
    add_filter( 'genesis_post_title_text', 'child_remove_stupidword_from_titles' );

    #92125

    hansel
    Member
    Post count: 4

    For me this removes the entire title… I just want the last word of the title removed…

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.