the_title vs the_title_attribute (6 posts)

  1. Ghodmode
    Posted 7 years ago #

    What is the intended difference between the functions the_title_attribute() and the_title()?

    I'm trying to create my own simple custom theme using the default theme as a guide. Right now, both functions are producing the same output, but I suspect there might be a set of circumstances where they would produce different output.

    Thank you,

  2. Adam Brown
    Posted 7 years ago #

    Maybe somebody who knows better can confirm this, but if I remember right, the_title_attribute produces a "cleaner" version of the_title that can be placed into something like this:

    <a href="" title="<?php the_title_attribute() ?>"><?php the_title(); ?></a>

    I'm hoping you see the distinction in how I used them--one of them is used within the <a> tag (as an attribute), and one is used as text.

  3. Kafkaesqui

    Posted 7 years ago #

    the_title_attribute() accepts its parameters in query-string style format. So instead of:

    <?php the_title('<h3>', '</h3>'); ?>

    you would do:

    <?php the_title_attribute('before=<h3>&after=</h3>'); ?>

    Otherwise, they're exactly the same. (Seems the_title_attribute() is missing in Codex. Will have to fix that...)

    EDIT: Dug a bit and see adamrbrown is correct as well.

    the_title_attribute() does clean things up, escaping (or rather, converting) certain characters to character entity equivalents, and stripping out any HTML tags.

  4. bmck
    Posted 7 years ago #

    I had some validation errors, and using the_title_attribute within anchors cleared these up. Shame, however, it doesn't remove spaces. For example, this:

    <a href="mailto:?subject=<?php the_title_attribute(); ?>&amp;body=See:%20<?php the_permalink(); ?>Send</a>

    Might produce the following (improperly encoded due to spaces in title - as there nearly always are) URL:

    <a href="mailto:?subject=Oh no I have gaps&amp;body=See:%20http://...">Send</a>

    We need a parameter to make it produce valid anchors e.g.:

    <a href="mailto:?subject=Oh%20no%20I%20have%20gaps&amp;body=See:%20http://...">Send</a>

  5. Adam Brown
    Posted 7 years ago #

    Both functions are defined in wp-includes/template-functions-post.php. Take a look at them and create a custom function based on them. Put it in your theme's functions.php. Maybe something like this:

    if (!function_exists('the_title_mailto')){
    	function the_title_mailto(){
    		if ( strlen($title) > 0 ){
    			$title = apply_filters('the_title', $title);
    			$title = urlencode($title); // fix spaces
    		echo $title;
  6. bmck
    Posted 7 years ago #

    Thanks Adam; I put a custom function into the loop and tried out various adaptations of your code above in my theme's functions.php file, but just couldn't get it to work (I'm a PHP novice).

    Adapting this fix I tried the following:

    <?php _e(urlencode(the_title_attribute('echo=0'))); ?>

    It works (that is it satisfies "Check syntax" in BBEdit and Tidy; it's worth noting that the malformed URI fools the W3C validator - which is probably why nobody much cares!).

    However, this encodes the spaces with "+". For example the title of this post, when I click "Send" (Actions box), becomes:


    I'd rather see:

    From Proto.aculo.us to jQuery

    Any further suggestions?

    BTW the functions are defined in wp-includes/post-template.php in WP 2.3.2

Topic Closed

This topic has been closed to new replies.

About this Topic