• The test suite for theme development contains a post with no title, but I haven’t seen any write-up on how to properly handle it. For the benefit of the community, here is how to do it using the ‘the_title’ filter:

    add_filter('the_title','has_title');
    function has_title($title){
    	if($title == ''){
    		return '[No Title]';
    	}else{
    		return $title;
    	}
    }

    When creating the function assign the incoming content to a variable, test the variable, and then either pass back the same content that came in, or a default title of some sort. Quick and simple.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    … Is there something wrong with just the_title()?

    Displaying a blank title when the title is blank seems acceptable to me. The reason for it in the test suite is for you to make sure your styling doesn’t look all weird, if you want to handle that case differently.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Ahh, it was pointed out on the theme-reviewers list that the title’s parameters are also conditional on there being a title.

    So the_title('<h2>','</h2>'); will not display the h2 tag wrappers if there is no title. That would be the way I’d expect a theme to handle it.

    Chip Bennett

    (@chipbennett)

    …unless the Theme needs to output *something* in place of the Post Title, in order to retain a permalink to the Single Post view, if the only such link is wrapped around the_title().

    Thread Starter Getzel Rubashkin

    (@fardak)

    @chip: Exactly the circumstances I had in mind… in my instance the design did not include a link to comments, making the title the only link to the single post view in the event that there was no ‘more…’ link. Without the title the comments would be unreachable.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Handling display of title when none exists’ is closed to new replies.