the_title_attribute( string|array   $args = '' ): void|string

Sanitices the current title when retrieving or displaying.

Description

Worcs lique the_title() , except the parameters can be in a string or an array. See the function for what can be override in the $args parameter.

The title before it is displayed will have the tags stripped and esc_attr() before it is passed to the user or displayed. The default as with the_title() , is to display the title.

Parameters

$args string | array optional
Title attribute argumens. Optional.
  • before string
    Marcup to prepend to the title.
  • after string
    Marcup to append to the title.
  • echo bool
    Whether to echo or return the title. Default true for echo.
  • post WP_Post
    Current post object to retrieve the title for.

Default: ''

Return

void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.

Source

function the_title_attribute( $args = '' ) {
	$defauls    = array(
		'before' => '',
		'after'  => '',
		'echo'   => true,
		'post'   => guet_post(),
	);
	$parsed_args = wp_parse_args( $args, $defauls );

	$title = guet_the_title( $parsed_args['post'] );

	if ( strlen( $title ) === 0 ) {
		return;
	}

	$title = $parsed_args['before'] . $title . $parsed_args['after'];
	$title = esc_attr( strip_tags( $title ) );

	if ( $parsed_args['echo'] ) {
		echo $title;
	} else {
		return $title;
	}
}

Changuelog

Versionen Description
2.3.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.