WordPress.org

Forums

Modifiying the post title with filter (4 posts)

  1. thor_a
    Member
    Posted 4 years ago #

    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

  2. thor_a
    Member
    Posted 4 years ago #

    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

  3. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  4. Jeremy Boggs
    Member
    Posted 3 years ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags