Support » Plugins » Modifiying the post title with filter

  • I am trying to modify the post title with a filter. Titles of pages shall not be changed. I tried:


    add_filter( ‘the_title’, ‘ta_modified_post_title’);
    function ta_modified_post_title ($title) {
    $title = $title.’ (modified title)’;
    return $title;
    }

    .. but that did change the page titles, too.

    I tried different other filters (e.g. post_title, single_title) but without success. (Tried in the standard twentyten theme.)

    Is there any solution for this problem without going into the template files themselfs?

    Thank you for any help,

    Thorsten

Viewing 3 replies - 1 through 3 (of 3 total)
  • I found a part of the solution myself:

    add_filter( 'the_title', 'ta_modified_post_title');
    function ta_modified_post_title ($title) {
      if( $title == $post->post_title and !is_page() ){
        $title = $title." (modified title)";
      }
      return $title;
    }

    However, the modified title appears in widgets in the sidebar (e.g. last articles, comments). This should not happen.

    Why do I need this? I would like to insert some special html tags for the title to be able format it differently, e.g. with a colored background per category.

    Thorsten

    esmi

    (@esmi)

    Forum Moderator

    add_filter( 'the_title', 'ta_modified_post_title');
    function ta_modified_post_title ($title) {
      if( $title == $post->post_title and !is_page() ){
        $title = $title.'<span class="modified> (modified title)</span>';
      }
      return $title;
    }

    Then use CSS to hide (modified title) in the sidebar etc using .modified.

    You can also use the in_the_loop() conditional tag to only alter the title if you are in the loop, and not elsewhere in WP. Something like this might work:

    add_filter( 'the_title', 'ta_modified_post_title');
    function ta_modified_post_title ($title) {
      if ( in_the_loop() && !is_page() ) {
        $title = $title." (modified title)";
      }
      return $title;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Modifiying the post title with filter’ is closed to new replies.