Site logo

Archived topic

Add Title Attribute to Links

10 replies · Started by stephen on May 31, 2020

Viewing posts 1–11 of 11

Was wondering if there is a way to add the "title" attribute to links on the links on the main page?

For example, we are looking to have the page title for the links on the main page to automatically have the "title="PAGE-TITLE-HERE attribute. Is there a way to make it automatically display like that on the archived/main page links?

Anything on this?

Hi there,

This is for the post titles? There isn't a filter for this at the moment.

Wouldn't the title attribute be identical to the actual title of the post that's already displaying?

Yes and yes. We are trying to add the "title" attribute to links on the main page/archived pages for SEO.

In that case, you would likely need to copy the content.php from the parent theme and add it to your child theme. Then you could add the title attribute to this line: https://github.com/tomusborne/generatepress/blob/2.4.2/content.php#L34

So it would look like this:

the_title( 
    sprint( 
        '<h2 class="entry-title" itemprop="headline"><a href="%1$s" rel="bookmark" title="%2$s>', 
        esc_url( get_permalink() ),
        the_title_attribute( 'echo=0' )
    ), 
    '</a></h2>' 
);

I got it to work with a few tweaks to the code above wit hthe following:

the_title( sprintf( '<h2 class="entry-title" itemprop="headline"><a href="%s" rel="bookmark" title="%2$s">', esc_url( get_permalink() ), the_title_attribute( 'echo=0' ) ), '</a></h2>' );

Thank you so much for helping. You guys are the best.

Glad I could help :)

Hey, we just updated to the latest theme and was wondering how we could achieve this again? The code is slightly different now on the new theme.

Hi,

This should PHP snippet should achieve the same result.

add_filter( 'generate_get_the_title_parameters', function( $params ) {
    if ( ! is_singular() ) {
        $params = array(
	    'before' => sprintf(
	        '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark" target="_blank" title="%3$s">',
		esc_url( get_permalink() ),
		'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '',
		the_title_attribute( 'echo=0' )
	    ),
	    'after' => '</a></h2>',
	);
    }
    return $params;
} );

Thank you!

@Melissa No problem. Let us know if you need further help. :D

This archived topic is closed to new replies.