[Support request] Add Title Attribute to Links

Home Forums Support Add Title Attribute to Links

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1309330
    stephen

    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?

    #1309989
    stephen

    Anything on this?

    #1310068
    Tom
    Lead Developer
    Lead Developer

    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?

    #1310073
    stephen

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

    #1310399
    Tom
    Lead Developer
    Lead Developer

    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>' 
    );
    #1310408
    stephen

    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.

    #1311487
    Tom
    Lead Developer
    Lead Developer
    #1619048
    stephen

    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.

    #1619407
    Elvin
    Customer Support

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

    A wise man once said:
    "Have you cleared your cache?"

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.